# --------------------------------------------------------
# Copyright (C) 2004-2020 by EMGU Corporation. All rights reserved.
# 
# Root CMake file for Emgu CV
#
# From the off-tree build directory, invoke:
#   $ cmake <PATH_TO_EMGUCV_ROOT>
#
# --------------------------------------------------------

PROJECT(emgucv)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.1)

IF( WIN32 OR ANDROID OR APPLE OR NETFX_CORE)
	SET(EMGU_CV_WITH_FREETYPE ON CACHE BOOL "BUILD FREETYPE")
ELSE() 
	SET(EMGU_CV_WITH_FREETYPE OFF CACHE BOOL "BUILD FREETYPE")
ENDIF()

IF (EMGU_CV_WITH_FREETYPE)
   IF (IOS)
    MESSAGE(STATUS "Setting up freetype for IOS ")
	
	#FIND_PACKAGE(FREETYPE)
	IF(NOT FREETYPE_FOUND)
	  
	  #MESSAGE(STATUS "((((((((((((((((((( FREETYPE2 added )))))))))))))))))))")
	  ADD_SUBDIRECTORY(freetype2)
	  
	  SET(FREETYPE_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/freetype2/include")
	  SET(FREETYPE_LIBRARIES "freetype")
	  SET(FREETYPE_LIBRARY "freetype")

	  #SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_BINARY_DIR}/freetype2/CMakeFiles/Export/lib/cmake/freetype")
	  #SET(freetype_DIR "${CMAKE_BINARY_DIR}/freetype2/CMakeFiles/Export/lib/cmake/freetype")
	  FIND_PACKAGE(freetype)
	  SET(FREETYPE_FOUND ON)

	ENDIF()
	
	#FIND_PACKAGE(HARFBUZZ)
	IF(NOT HARFBUZZ_FOUND)
	  SET(HB_HAVE_FREETYPE ON) 
	  SET(HARFBUZZ_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/harfbuzz/src")
	  SET(HARFBUZZ_LIBRARIES "harfbuzz")
	  
	  ADD_SUBDIRECTORY(harfbuzz)
	  target_compile_definitions(harfbuzz PUBLIC HB_NO_VISIBILITY)
	  #SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_BINARY_DIR}/harfbuzz/CMakeFiles/Export/lib/cmake/harfbuzz")
	  #SET(harfbuzz_DIR "${CMAKE_BINARY_DIR}/harfbuzz/CMakeFiles/Export/lib/cmake/harfbuzz")
	  MESSAGE(STATUS "harfbuzz_DIR: ${harfbuzz_DIR}")
	  FIND_PACKAGE(harfbuzz)
	  SET(HARFBUZZ_FOUND ON)
	ENDIF()

   ELSEIF (NETFX_CORE OR ANDROID)
    MESSAGE(STATUS "Setting up freetype for NETFX_CORE / Android / IOS ")
	
	#FIND_PACKAGE(FREETYPE)
	IF(NOT FREETYPE_FOUND)
	  
	  #MESSAGE(STATUS "((((((((((((((((((( FREETYPE2 added )))))))))))))))))))")
	  ADD_SUBDIRECTORY(freetype2)
	  
	  SET(FREETYPE_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/freetype2/include")
	  SET(FREETYPE_LIBRARIES "freetype")
	  SET(FREETYPE_LIBRARY "freetype")

	  #SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_BINARY_DIR}/freetype2/CMakeFiles/Export/lib/cmake/freetype")
	  #SET(freetype_DIR "${CMAKE_BINARY_DIR}/freetype2/CMakeFiles/Export/lib/cmake/freetype")
	  FIND_PACKAGE(freetype)
	  SET(FREETYPE_FOUND ON)

	ENDIF()
	
	#FIND_PACKAGE(HARFBUZZ)
	IF(NOT HARFBUZZ_FOUND)
	  SET(HB_HAVE_FREETYPE ON) 
	  SET(HARFBUZZ_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/harfbuzz/src")
	  SET(HARFBUZZ_LIBRARIES "harfbuzz")
	  
	  ADD_SUBDIRECTORY(harfbuzz)
	  #SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_BINARY_DIR}/harfbuzz/CMakeFiles/Export/lib/cmake/harfbuzz")
	  #SET(harfbuzz_DIR "${CMAKE_BINARY_DIR}/harfbuzz/CMakeFiles/Export/lib/cmake/harfbuzz")
	  MESSAGE(STATUS "harfbuzz_DIR: ${harfbuzz_DIR}")
	  FIND_PACKAGE(harfbuzz)
	  SET(HARFBUZZ_FOUND ON)
	ENDIF()

  ELSEIF(WIN32 OR APPLE)
	MESSAGE(STATUS "Setting up freetype for Windows / Mac")
	    SET(FREETYPE_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/freetype2/include")
		SET(FREETYPE_LIBRARIES "freetype" CACHE INTERNAL "")
		SET(FREETYPE_LINK_LIBRARIES_XXXXX ON)
		SET(FREETYPE_LINK_LIBRARIES "freetype")
		SET(FREETYPE_LIBRARY "freetype")
		ADD_SUBDIRECTORY(freetype2)
		FIND_PACKAGE(freetype)
		SET(FREETYPE_FOUND ON)
		
		SET(HB_HAVE_FREETYPE ON)
		SET(HARFBUZZ_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/harfbuzz/src")
		SET(HARFBUZZ_LIBRARIES "harfbuzz" CACHE INTERNAL "")
		SET(HARFBUZZ_LINK_LIBRARIES_XXXXX ON)
		SET(HARFBUZZ_LINK_LIBRARIES "harfbuzz")
		SET(HARFBUZZ_LIBRARY "harfbuzz")
		ADD_SUBDIRECTORY(harfbuzz)
		SET(HARFBUZZ_FOUND ON)
		
  ELSE()
  ENDIF()
ELSE()
  SET(FREETYPE_LIBRARY "")
ENDIF()

IF( NOT (NETFX_CORE OR ANDROID OR IOS ) )
  
  #  IF (NOT WIN32)
  #    SET(CMAKE_C_FLAGS_RELEASE "-fPIC ${CMAKE_C_FLAGS_RELEASE}")
  #    SET(CMAKE_CXX_FLAGS_RELEASE "-fPIC ${CMAKE_CXX_FLAGS_RELEASE}")
  #    SET(CMAKE_C_FLAGS_DEBUG "-fPIC ${CMAKE_C_FLAGS_DEBUG}")
  #    SET(CMAKE_CXX_FLAGS_DEBUG "-fPIC ${CMAKE_CXX_FLAGS_DEBUG}")
  #    SET(CMAKE_EXE_LINKER_FLAGS "-fPIC ${CMAKE_EXE_LINKER_FLAGS}")
  #    SET(CMAKE_SHARED_LINKER_FLAGS "-fPIC ${CMAKE_SHARED_LINKER_FLAGS}")
  #  ENDIF()
  IF (WIN32)
    SET(BUILD_SHARED_LIBS_CACHE ${BUILD_SHARED_LIBS})
    SET(BUILD_SHARED_LIBS OFF)
    SET(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)
    
    ADD_SUBDIRECTORY(vtk) # always build vtk as static library
	
    
    SET(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_CACHE})
  ENDIF()
  #  IF (NOT WIN32)
  #    SET(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE_CACHE} )
  #    SET(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE_CACHE})
  #  ENDIF()
ENDIF()

SET(OPENCV_SUBFOLDER ${CMAKE_CURRENT_SOURCE_DIR}/opencv)
SET(OPENCV_CONTRIB_SUBFOLDER ${CMAKE_CURRENT_SOURCE_DIR}/opencv_contrib)
SET(OPENCV_EXTRA_SUBFOLDER ${CMAKE_CURRENT_SOURCE_DIR}/opencv_extra)

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
#IF (NOT ANDROID)
  
  SET(CSC_PREFERRED_VERSION "4.0")
  IF((NOT NETFX_CORE) AND MSVC) 
    #SET CSC 3.5 as the prefered compiler for EMGU CV
    SET(CSC_TARGET_FRAMEWORK "3.5" CACHE STRING "C#  target framework")
  ELSE()
    #SET CSC 4.5 (in the .Net framework 4.0 folder) as the prefered compiler for EMGU CV
    SET(CSC_TARGET_FRAMEWORK "" CACHE STRING "C# target framework")
  ENDIF()
  
  FIND_PACKAGE(CSharp REQUIRED)
  INCLUDE (CSharpMacros)
#ENDIF()

INCLUDE (CheckTargetArch)
INCLUDE (OpenCVVersion)

SET(LIBRARY_3RDPARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/3rdParty)

# ----------------------------------------------------------------------------
#  Autodetect if we are in a GIT repository
# ----------------------------------------------------------------------------
FIND_PROGRAM(GIT_PATH git)
FIND_PROGRAM(WC_PATH wc PATHS "c:/cygwin64/bin" "c:/cygwin/bin")
MARK_AS_ADVANCED(force GIT_PATH WC_PATH)
IF(GIT_PATH AND WC_PATH)
  MESSAGE(STATUS "Extracting Emgu CV git version, please wait...")
  EXECUTE_PROCESS(
    WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
    COMMAND ${GIT_PATH} log --oneline 
    COMMAND ${WC_PATH} -l
    OUTPUT_VARIABLE GITVERSION_RESULT)
  #MESSAGE(STATUS "COMMAND: ${GIT_PATH} log --oneline | ${WC_PATH} -l")
  STRING(REGEX REPLACE "^([0123456789]+).*" "\\1" EMGUCV_GITVERSION "${GITVERSION_RESULT}")
  STRING(STRIP "${EMGUCV_GITVERSION}" EMGUCV_GITVERSION)
  IF(EMGUCV_GITVERSION MATCHES "^$" OR EMGUCV_GITVERSION MATCHES "^-")
    # This is NOT a git repository:
    SET(EMGUCV_GITVERSION "0")
  ENDIF()
  MESSAGE(STATUS "Emgu CV GIT VERSION: ${EMGUCV_GITVERSION}")
ELSE()
  # We don't have git or wc:
  SET(EMGUCV_GITVERSION "0")
ENDIF()

# ----------------------------------------------------------------------------
#  Check if 32bit or 64-bit
# ----------------------------------------------------------------------------
CHECK_TARGET_ARCH()

IF (WIN32)
  MESSAGE(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
  MESSAGE(STATUS "CMAKE_GENERATOR: ${CMAKE_GENERATOR}")
  #STRING(FIND "${CMAKE_GENERATOR}" "ARM" IS_ARM)
  IF(TARGET_ARCH_64)
	IF(TARGET_ARM)
      SET(EMGUCV_ARCH arm64)
    ELSE()
      SET(EMGUCV_ARCH win64)
	ENDIF()
  ELSE()
    IF(TARGET_ARM)
      SET(EMGUCV_ARCH arm)
    ELSE()
      SET(EMGUCV_ARCH win32)
    ENDIF()
  ENDIF()
ELSE(WIN32)
  SET(EMGUCV_ARCH ${CMAKE_SYSTEM_NAME})
ENDIF(WIN32)

MESSAGE(STATUS "Building for ${EMGUCV_ARCH}")

IF(WIN32 AND CV_ICC)
  #  SET(CV_ICC_ADDITIONAL_FLAGS_RELEASE "/O3 /QaxCORE-AVX2,AVX,SSE4.1,SSE3 /Qparallel /Qopt-report:1 /Qopt-report-phase:vec /Qopt-report-phase:par /Qopt-report-file:stderr /Qipo /Qunroll-aggressive")
  SET(CV_ICC_ADDITIONAL_FLAGS_RELEASE "/O3 /QaxCORE-AVX2,AVX,SSE4.1,SSE3 /Qparallel /Qopt-report:1 /Qopt-report-phase:vec /Qopt-report-phase:par /Qipo /Qrestrict /Ot")
  STRING(REGEX REPLACE "/O2" ${CV_ICC_ADDITIONAL_FLAGS_RELEASE} CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
  STRING(REGEX REPLACE "/O2" ${CV_ICC_ADDITIONAL_FLAGS_RELEASE} CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
  SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:\"libcmt\"")
  SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NODEFAULTLIB:\"libcmt\"")
  set(EMGUCV_PLATFORM_TOOLSET "Intel C++ Compiler 19.1" CACHE STRING "Platform toolset for Visual Studio")
ENDIF()

IF(WIN32 AND NOT MINGW)
  SET(NETFX_CORE OFF CACHE BOOL "Build Windows Store App")
  #  IF(NETFX_CORE)
  #    add_definitions(-DWINAPI_FAMILY=WINAPI_FAMILY_APP -DWINVER=0x0602 -D_WIN32_WINNT=0x0602) 
  #	add_definitions(-D_UNICODE)
  #  ENDIF()
ENDIF()

IF(IOS)
  SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/libs)
ELSE()
  SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/libs)
ENDIF()

IF(NOT WIN32)
  SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/libs)
ENDIF()

IF(ANDROID)
  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/android/${ANDROID_ABI}")
ELSEIF(WIN32)
  IF ("${EMGUCV_ARCH}" STREQUAL "win64")
    IF("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore" AND (CMAKE_SYSTEM_VERSION MATCHES 10.0))
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/UWP/x64")
    ELSE()
      SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/x64")
    ENDIF()
  ELSEIF("${EMGUCV_ARCH}" STREQUAL "arm64")
    IF("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore" AND (CMAKE_SYSTEM_VERSION MATCHES 10.0))
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/UWP/arm64")
    ELSE()
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/arm64")
    ENDIF()
  ELSEIF("${EMGUCV_ARCH}" STREQUAL "arm")
    IF("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore" AND (CMAKE_SYSTEM_VERSION MATCHES 10.0))
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/UWP/arm")
    ELSE()
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/arm")
    ENDIF()
  ELSE()
    IF("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore" AND (CMAKE_SYSTEM_VERSION MATCHES 10.0))
	#IF("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
    #  IF(CMAKE_SYSTEM_VERSION MATCHES 10.0)
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/UWP/x86")    
	ELSE()
	  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/x86")
	ENDIF()
  ENDIF()
  #MESSAGE(STATUS ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
  #MESSAGE(STATUS ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> EMGUCV_ARCH: ${EMGUCV_ARCH}")
  #MESSAGE(STATUS ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER: ${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER}")
ELSEIF(APPLE AND NOT IOS)
  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "/osx")
ELSE()
  SET(UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER "")
ENDIF()

SET(UNMANAGED_LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/libs${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER}")
IF(ANDROID)
  SET(LIBRARY_OUTPUT_PATH ${UNMANAGED_LIBRARY_OUTPUT_PATH})
ENDIF()
FILE(MAKE_DIRECTORY ${UNMANAGED_LIBRARY_OUTPUT_PATH})

SET(UNMANAGED_DLL_EXTRA)

MACRO(CHECK_BINARY_EXIST folder pattern property_name second_check)
  #IF("${UNMANAGED_LIBRARY_OUTPUT_PATH}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/${folder}")
  #  SET(${property_name} ON)
  #ELSE()
    FILE(GLOB DLL_LIST "${CMAKE_CURRENT_SOURCE_DIR}/${folder}/${pattern}")
    LIST(LENGTH DLL_LIST DLL_LIST_LENGTH)
    IF(DLL_LIST_LENGTH GREATER 0)
      LIST(APPEND UNMANAGED_DLL_EXTRA ${DLL_LIST})
      MESSAGE(STATUS "Including the following files in package: ${DLL_LIST}")
      INSTALL(
	   FILES
	   ${DLL_LIST}
	   DESTINATION ${folder}
	   COMPONENT emgucv_binary
	   )  
      SET(${property_name} ON)
    ELSE()
      SET(${property_name} ${second_check})
    ENDIF()
  #ENDIF()
  #MESSAGE(STATUS "Property - ${property_name}: ${${property_name}}")
  IF(${${property_name}})
    MESSAGE(STATUS "${property_name}: ${${property_name}}")
  ENDIF()
ENDMACRO()

#MESSAGE(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
SET(IS_IOS_BUILD OFF)
IF(IOS)
  SET(IS_IOS_BUILD ON)
  MESSAGE(STATUS "IS_IOS_BUILD: ${IS_IOS_BUILD}")
  
  SET(IS_IOS_I386_BUILD OFF)
  IF(IS_IOS_BUILD AND ("${IOS_ARCH}" STREQUAL "i386"))
    SET(IS_IOS_I386_BUILD ON)
    MESSAGE(STATUS "IS_IOS_I386_BUILD: ${IS_IOS_I386_BUILD}")
  ENDIF()
  #CHECK_BINARY_EXIST("libs/iOS" "libcvextern_i386.a" HAVE_IOS_I386 ${IS_IOS_I386_BUILD})
  
  SET(IS_IOS_X86_64_BUILD OFF)
  IF(IS_IOS_BUILD AND ("${IOS_ARCH}" STREQUAL "x86_64"))
    SET(IS_IOS_X86_64_BUILD ON)
    MESSAGE(STATUS "IS_IOS_X86_64_BUILD: ${IS_IOS_X86_64_BUILD}")
  ENDIF()
  #CHECK_BINARY_EXIST("libs/iOS" "libcvextern_x86_64.a" HAVE_IOS_X86_64 ${IS_IOS_X86_64_BUILD})
  
  SET(IS_IOS_ARMV7S_BUILD OFF)
  IF(IS_IOS_BUILD AND ("${IOS_ARCH}" STREQUAL "armv7s"))
    SET(IS_IOS_ARMV7S_BUILD ON)
    MESSAGE(STATUS "IS_IOS_ARMV7S_BUILD: ${IS_IOS_ARMV7S_BUILD}")
  ENDIF()
  #CHECK_BINARY_EXIST("libs/iOS" "libcvextern_armv7s.a" HAVE_IOS_ARMV7S ${IS_IOS_ARMV7S_BUILD})
  
  SET(IS_IOS_ARMV7_BUILD OFF)
  IF(IS_IOS_BUILD AND ("${IOS_ARCH}" STREQUAL "armv7"))
    SET(IS_IOS_ARMV7_BUILD ON)
    MESSAGE(STATUS "IS_IOS_ARMV7_BUILD: ${IS_IOS_ARMV7_BUILD}")
  ENDIF()
  #CHECK_BINARY_EXIST("libs/iOS" "libcvextern_armv7.a" HAVE_IOS_ARMV7 ${IS_IOS_ARMV7_BUILD})
  
  SET(IS_IOS_ARM64_BUILD OFF)
  IF(IS_IOS_BUILD AND ("${IOS_ARCH}" STREQUAL "arm64"))
    SET(IS_IOS_ARM64_BUILD ON)
    MESSAGE(STATUS "IS_IOS_ARM64_BUILD: ${IS_IOS_ARM64_BUILD}")
  ENDIF()
  #CHECK_BINARY_EXIST("libs/iOS" "libcvextern_arm64.a" HAVE_IOS_ARM64 ${IS_IOS_ARM64_BUILD})
ENDIF()
CHECK_BINARY_EXIST("libs/iOS" "libcvextern.a" HAVE_IOS ${IS_IOS_BUILD})

#IF(NOT HAVE_IOS)
#  IF(HAVE_IOS_I386 OR HAVE_IOS_X86_64 OR HAVE_IOS_ARMV7S OR HAVE_IOS_ARMV7 OR HAVE_IOS_ARM64)
#    SET(HAVE_IOS ON) 
#  ENDIF()
#ENDIF()

#SET(HAVE_IOS_ALL_ARCH OFF)
#IF(HAVE_IOS_I386 AND HAVE_IOS_X86_64 AND HAVE_IOS_ARMV7S AND HAVE_IOS_ARMV7 AND HAVE_IOS_ARM64)
#  SET(HAVE_IOS_ALL_ARCH ON) 
#ENDIF()
#IF(HAVE_IOS_ALL_ARCH)
#  MESSAGE(STATUS "HAVE_IOS_ALL_ARCH: ${HAVE_IOS_ALL_ARCH}")
#ENDIF()

IF(HAVE_IOS)
  MESSAGE(STATUS "HAVE_IOS: ON")
ENDIF()


SET(IS_OSX_BUILD OFF)
IF(APPLE AND (NOT IOS))
  SET(IS_OSX_BUILD ON)
  MESSAGE(STATUS "IS_OSX_BUILD: ${IS_OSX_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/osx" "libcvextern.dylib" HAVE_OSX ${IS_OSX_BUILD})

SET(IS_ANDROID_ARM64_V8A_BUILD OFF)
IF(ANDROID AND ("${ANDROID_ABI}" STREQUAL "arm64-v8a"))
  SET(IS_ANDROID_ARM64_V8A_BUILD ON)
  MESSAGE(STATUS "IS_ANDROID_ARM64_V8A_BUILD: ${IS_ANDROID_ARM64_V8A_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/android/arm64-v8a" "*.so" HAVE_ANDROID_ARM64_V8A ${IS_ANDROID_ARM64_V8A_BUILD})

#SET(IS_ANDROID_ARMEABI_BUILD OFF)
#IF(ANDROID AND ("${ANDROID_ABI}" STREQUAL "armeabi"))
#  SET(IS_ANDROID_ARMEABI_BUILD ON)
#  MESSAGE(STATUS "IS_ANDROID_ARMEABI_BUILD: ${IS_ANDROID_ARMEABI_BUILD}")
#ENDIF()
#CHECK_BINARY_EXIST("libs/android/armeabi" "*.so" HAVE_ANDROID_ARMEABI ${IS_ANDROID_ARMEABI_BUILD})

SET(IS_ANDROID_ARMEABI_V7A_BUILD OFF)
IF(ANDROID AND ("${ANDROID_ABI}" STREQUAL "armeabi-v7a"))
  SET(IS_ANDROID_ARMEABI_V7A_BUILD ON)
  MESSAGE(STATUS "IS_ANDROID_ARMEABI_V7A_BUILD: ${IS_ANDROID_ARMEABI_V7A_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/android/armeabi-v7a" "*.so" HAVE_ANDROID_ARMEABI_V7A ${IS_ANDROID_ARMEABI_V7A_BUILD})

SET(IS_ANDROID_X86_BUILD OFF)
IF(ANDROID AND ("${ANDROID_ABI}" STREQUAL "x86"))
  SET(IS_ANDROID_X86_BUILD ON)
  MESSAGE(STATUS "IS_ANDROID_X86_BUILD: ${IS_ANDROID_X86_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/android/x86" "*.so" HAVE_ANDROID_X86 ${IS_ANDROID_X86_BUILD})

SET(IS_ANDROID_X86_64_BUILD OFF)
IF(ANDROID AND ("${ANDROID_ABI}" STREQUAL "x86_64"))
  SET(IS_ANDROID_X86_64_BUILD ON)
  MESSAGE(STATUS "IS_ANDROID_X86_64_BUILD: ${IS_ANDROID_X86_64_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/android/x86_64" "*.so" HAVE_ANDROID_X86_64 ${IS_ANDROID_X86_64_BUILD})

SET(HAVE_ANDROID OFF)
#IF(HAVE_ANDROID_ARM64_V8A OR HAVE_ANDROID_ARMEABI OR HAVE_ANDROID_ARMEABI_V7A OR HAVE_ANDROID_X86 OR HAVE_ANDROID_X86_64)
IF(HAVE_ANDROID_ARM64_V8A OR HAVE_ANDROID_ARMEABI_V7A OR HAVE_ANDROID_X86 OR HAVE_ANDROID_X86_64)
  SET(HAVE_ANDROID ON) 
ENDIF()
IF(HAVE_ANDROID)
  MESSAGE(STATUS "HAVE_ANDROID: ON")
ENDIF()

SET(HAVE_ANDROID_ALL_ARCH OFF)
#IF(HAVE_ANDROID_ARM64_V8A AND HAVE_ANDROID_ARMEABI AND HAVE_ANDROID_ARMEABI_V7A AND HAVE_ANDROID_X86 AND HAVE_ANDROID_X86_64)
IF(HAVE_ANDROID_ARM64_V8A AND HAVE_ANDROID_ARMEABI_V7A AND HAVE_ANDROID_X86 AND HAVE_ANDROID_X86_64)
  SET(HAVE_ANDROID_ALL_ARCH ON) 
ENDIF()
IF(HAVE_ANDROID_ALL_ARCH)
  MESSAGE(STATUS "HAVE_ANDROID_ALL_ARCH: ON")
ENDIF()


SET(IS_WINSTORE_10_X86_BUILD OFF)
IF( ("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore") AND (CMAKE_SYSTEM_VERSION MATCHES 10.0) AND ("${EMGUCV_ARCH}" STREQUAL "win32") )
  SET(IS_WINSTORE_10_X86_BUILD ON)
  MESSAGE(STATUS "IS_WINSTORE_10_X86_BUILD: ${IS_WINSTORE_10_X86_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/UWP/x86" "*.dll" HAVE_WINSTORE_10_X86 ${IS_WINSTORE_10_X86_BUILD})

SET(IS_WINSTORE_10_X64_BUILD OFF)
IF( ("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore") AND (CMAKE_SYSTEM_VERSION MATCHES 10.0) AND ("${EMGUCV_ARCH}" STREQUAL "win64") )
  SET(IS_WINSTORE_10_X64_BUILD ON)
  MESSAGE(STATUS "IS_WINSTORE_10_X64_BUILD: ${IS_WINSTORE_10_X64_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/UWP/x64" "*.dll" HAVE_WINSTORE_10_X64 ${IS_WINSTORE_10_X64_BUILD})

SET(IS_WINSTORE_10_ARM_BUILD OFF)
IF(("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore") AND (CMAKE_SYSTEM_VERSION MATCHES 10.0) AND ("${EMGUCV_ARCH}" STREQUAL "arm"))
  SET(IS_WINSTORE_10_ARM_BUILD ON)
  MESSAGE(STATUS "IS_WINSTORE_10_ARM_BUILD: ${IS_WINSTORE_10_ARM_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/UWP/arm" "*.dll" HAVE_WINSTORE_10_ARM ${IS_WINSTORE_10_ARM_BUILD})

SET(IS_WINSTORE_10_ARM64_BUILD OFF)
IF(("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore") AND (CMAKE_SYSTEM_VERSION MATCHES 10.0) AND ("${EMGUCV_ARCH}" STREQUAL "arm64"))
  SET(IS_WINSTORE_10_ARM64_BUILD ON)
  MESSAGE(STATUS "IS_WINSTORE_10_ARM64_BUILD: ${IS_WINSTORE_10_ARM64_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/UWP/arm64" "*.dll" HAVE_WINSTORE_10_ARM64 ${IS_WINSTORE_10_ARM64_BUILD})

SET(IS_WINDESKTOP_X86_BUILD OFF)
IF (WIN32 AND ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") AND ("${EMGUCV_ARCH}" STREQUAL "win32") AND (NOT ANDROID)) 
  SET (IS_WINDESKTOP_X86_BUILD ON)
  MESSAGE(STATUS "IS_WINDESKTOP_X86_BUILD: ${IS_WINDESKTOP_X86_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/x86" "*.dll" HAVE_WINDESKTOP_X86 ${IS_WINDESKTOP_X86_BUILD})

SET (IS_WINDESKTOP_X64_BUILD OFF)
IF (WIN32 AND ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") AND ("${EMGUCV_ARCH}" STREQUAL "win64") AND (NOT ANDROID)) 
  SET (IS_WINDESKTOP_X64_BUILD ON)
  MESSAGE(STATUS "IS_WINDESKTOP_X64_BUILD: ${IS_WINDESKTOP_X64_BUILD}")
ENDIF()
CHECK_BINARY_EXIST("libs/x64" "*.dll" HAVE_WINDESKTOP_X64 ${IS_WINDESKTOP_X64_BUILD})
 
  IF(HAVE_WINDESKTOP_X86 AND HAVE_WINDESKTOP_X64)
    SET(WINDESKTOP_LABEL "-windesktop") 
  ELSEIF(HAVE_WINDESKTOP_X86)
    SET(WINDESKTOP_LABEL "-windesktop_x86")
  ELSEIF(HAVE_WINDESKTOP_X64)	
    SET(WINDESKTOP_LABEL "-windesktop_x64")
  ELSE() 
    SET(WINDESKTOP_LABEL "")
  ENDIF()
  
  SET(WINSTORE_10_LABEL "")
  IF(HAVE_WINSTORE_10_X64)
    SET(WINSTORE_10_LABEL "${WINSTORE_10_LABEL}_x64")
  ENDIF()
  IF(HAVE_WINSTORE_10_X86)
    SET(WINSTORE_10_LABEL "${WINSTORE_10_LABEL}_x86")
  ENDIF()
  IF(HAVE_WINSTORE_10_ARM)	
    SET(WINSTORE_10_LABEL "${WINSTORE_10_LABEL}_arm")
  ENDIF()
  IF(HAVE_WINSTORE_10_ARM64)	
    SET(WINSTORE_10_LABEL "${WINSTORE_10_LABEL}_arm64")
  ENDIF()
  
  SET(HAVE_WINSTORE_10 OFF)
  IF(HAVE_WINSTORE_10_X86 OR HAVE_WINSTORE_10_X64 OR HAVE_WINSTORE_10_ARM OR HAVE_WINSTORE_10_ARM64)
    SET(HAVE_WINSTORE_10 ON)
  ENDIF()
  
  IF("${WINSTORE_10_LABEL}" STREQUAL "_x64_x86_arm_arm64")
    SET(WINSTORE_10_LABEL "-UWP")
  ELSEIF(NOT ("${WINSTORE_10_LABEL}" STREQUAL ""))
    SET(WINSTORE_10_LABEL "-UWP${WINSTORE_10_LABEL}")
  ENDIF()
  
  SET(WINDOWS_LABEL "${WINSTORE_10_LABEL}${WINDESKTOP_LABEL}")
  IF ("${WINDOWS_LABEL}" STREQUAL "-UWP-windesktop")
    SET(WINDOWS_LABEL "-windows")
  ENDIF()
  
  SET(IOS_LABEL "")
  IF(HAVE_IOS)
    SET(IOS_LABEL "-ios")
  ENDIF()
  
  SET(OSX_LABEL "")
  IF(HAVE_OSX)
    SET(OSX_LABEL "-osx")
  ENDIF()	


SET(ANDROID_LABEL "")
IF(HAVE_ANDROID_ARM64_V8A)
  SET(ANDROID_LABEL "${ANDROID_LABEL}-arm64-v8a")
ENDIF()
#IF(HAVE_ANDROID_ARMEABI)
#  SET(ANDROID_LABEL "${ANDROID_LABEL}-armeabi")
#ENDIF()
IF(HAVE_ANDROID_ARMEABI_V7A)	
  SET(ANDROID_LABEL "${ANDROID_LABEL}-armeabi-v7a")
ENDIF()
IF(HAVE_ANDROID_X86)
  SET(ANDROID_LABEL "${ANDROID_LABEL}-x86")
ENDIF()
IF(HAVE_ANDROID_X86_64)
  SET(ANDROID_LABEL "${ANDROID_LABEL}-x86_64")
ENDIF()

#IF("${ANDROID_LABEL}" STREQUAL "-arm64-v8a-armeabi-armeabi-v7a-x86-x86_64")
IF("${ANDROID_LABEL}" STREQUAL "-arm64-v8a-armeabi-v7a-x86-x86_64")
  SET(ANDROID_LABEL "-android")
ELSEIF(NOT ("${ANDROID_LABEL}" STREQUAL ""))
  SET(ANDROID_LABEL "-android${ANDROID_LABEL}")
ENDIF()

CHECK_BINARY_EXIST("libs" "Emgu.CV.World*" HAVE_PREBUILD_EMGU_CV_WORLD OFF)

# ----------------------------------------------------------------------------
#  Setup Package information
# ----------------------------------------------------------------------------
SET(PACKAGE "${PROJECT_NAME}")
SET(CPACK_PACKAGE_CONTACT "Emgu CV SUPPORT <support@emgu.com>")
SET(PACKAGE_BUGREPORT "${CPACK_PACKAGE_CONTACT}")
SET(PACKAGE_NAME "${PROJECT_NAME}")

#SET(CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Emgu CV is a cross platform .Net wrapper to the OpenCV image processing library.")

SET(CPACK_PACKAGE_NAME "${PACKAGE_NAME}${IOS_LABEL}${ANDROID_LABEL}${WINDOWS_LABEL}${OSX_LABEL}")

#STRING(LENGTH "${CPACK_PACKAGE_NAME}" PACKAGE_NAME_LENGTH)
#IF (${PACKAGE_NAME_LENGTH} GREATER 30)
#   SET(CPACK_PACKAGE_NAME "${PACKAGE_NAME}-mixed")
#   STRING(LENGTH "${CPACK_PACKAGE_NAME}" PACKAGE_NAME_LENGTH)
#ENDIF()

#IF (APPLE)
#  SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-osx")
#ENDIF()

IF(WITH_CUDA)
  SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-cuda")
ENDIF()

IF(WITH_OPENNI) 
  SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-openni")
ENDIF()

IF(CV_ICC)
  SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-icc")
ENDIF()

IF(WITH_TBB)
  SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-tbb")
ENDIF()

IF(WITH_IPP)
  SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-ipp")
ENDIF()

IF("${CPACK_PACKAGE_NAME}" STREQUAL "${PACKAGE_NAME}-ios-android-windows-osx-icc-tbb-ipp")
  SET(CPACK_PACKAGE_NAME "${PACKAGE_NAME}-pro")
ENDIF()


SET(CPACK_PACKAGE_VENDOR "Emgu Corporation")
SET(CPACK_PACKAGE_VERSION_MAJOR ${OPENCV_VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${OPENCV_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${OPENCV_VERSION_PATCH})
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}.${EMGUCV_GITVERSION}")
IF(ANDROID)
  SET(CPACK_PACKAGE_FILE_NAME "lib${CPACK_PACKAGE_NAME}")
ELSE()
  SET(CPACK_PACKAGE_FILE_NAME "lib${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
ENDIF()

MESSAGE(STATUS "Package name will be ${CPACK_PACKAGE_NAME}")
FILE(WRITE "emgucv.version.txt" "${CPACK_PACKAGE_VERSION}")

#IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.Commercial.License.txt")
#  SET(LICENSE_FILE_NAME "Emgu.CV.Commercial.License.txt")
#ELSE()
SET(LICENSE_FILE_NAME "Emgu.CV.License.txt")

SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/${LICENSE_FILE_NAME}")

IF (WIN32 OR APPLE)
  SET(CPACK_PACKAGE_CLI_FOLDER bin)
ELSE ()
  SET(CPACK_PACKAGE_CLI_FOLDER lib/cli/${PACKAGE_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR})
ENDIF()

# ----------------------------------------------------------------------------
#  Build opencv and Emgu.CV.Extern
# ----------------------------------------------------------------------------
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${UNMANAGED_LIBRARY_OUTPUT_PATH}" )
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

#SET(CMAKE_ARCHIEVE_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
#SET(CMAKE_ARCHIEVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_ARCHIEVE_OUTPUT_DIRECTORY}")
#SET(CMAKE_ARCHIEVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_ARCHIEVE_OUTPUT_DIRECTORY}")

#SET(CMAKE_INSTALL_PREFIX_COPY ${CMAKE_INSTALL_PREFIX})
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER})
#SET(CPACK_PACKAGING_INSTALL_PREFIX ${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER})
#SET(DESTDIR ${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER})
#SET(CPACK_OUTPUT_FILE_PREFIX "x64")

SET(ENTRYPOINT_CS_STRING "")
SET(ENTRYPOINT_MODULES_STRING "")

#Add opencl
#IF(WIN32 AND (NOT NETFX_CORE) )
#  IF(WITH_OPENCL)
#    MESSAGE(STATUS "Open CL build included")
#	SET(OPENCL_SOURCE_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/lib/opencl-icd-1.2.11.0/icd")
#	ADD_SUBDIRECTORY(${OPENCL_SOURCE_FOLDER})
#	#GET_TARGET_PROPERTY(OPENCL_OUTPUT_NAME OpenCL OUTPUT_NAME)

#	set(OPENCL_FOUND YES)
#	set(OPENCL_LIBRARY "${CMAKE_CURRENT_SOURCE_DIR}/bin/Release/opencl.lib" CACHE STRING "OpenCL library")
#	set(OPENCL_INCLUDE_DIR "${OPENCL_SOURCE_FOLDER}/inc" CACHE STRING "OpenCL include directory")
#	mark_as_advanced(OPENCL_INCLUDE_DIR OPENCL_LIBRARY)

#	SET(ENTRYPOINT_MODULES_STRING "${ENTRYPOINT_MODULES_STRING}
#	     OpenclLibrary,")
#   SET(ENTRYPOINT_CS_STRING "${ENTRYPOINT_CS_STRING} 
#      /// <summary>
#      /// The file name of the OpenCL library
#      /// </summary>
#      public const string OpenclLibrary = \"OpenCL\";")
#  ELSE()
#    SET(OPENCL_OUTPUT_NAME "")
#	MESSAGE(STATUS "Open CL build not included")
#  ENDIF()
#ENDIF()

SET(CMAKE_OLD_SOURCE_DIR ${CMAKE_SOURCE_DIR})
SET(CMAKE_SOURCE_DIR ${OPENCV_SUBFOLDER})
IF(MSVC)
  set(ENABLE_SOLUTION_FOLDERS ON CACHE BOOL "Solution folder in Visual Studio")
ENDIF()
ADD_SUBDIRECTORY(${OPENCV_SUBFOLDER})
SET(CMAKE_SOURCE_DIR ${CMAKE_OLD_SOURCE_DIR})

SET (OPENCV_MODULE_NAME_CANDIDATES
  core hal imgproc video flann ml imgcodecs videoio highgui objdetect features2d calib3d
  cudaarithm cudawarping cudafilters cudaimgproc cudalegacy cudaoptflow 
  cudastereo cudabgsegm cudafeatures2d cudacodec cudaobjdetect
  photo superres  
  videostab shape
  cudev  
  face ximgproc xobjdetect xfeatures2d xphoto bgsegm optflow
  stitching latentsvm plot
  dnn dnn_objdetect dnn_superres aruco fuzzy viz bioinspired text tracking
  dpm
  saliency
  dnn_modern
  img_hash
  phase_unwrapping
  freetype
  hfs
  quality
  stereo
  surface_matching
  )

FOREACH(OPENCV_MODULE_NAME ${OPENCV_MODULE_NAME_CANDIDATES})
  IF(TARGET opencv_${OPENCV_MODULE_NAME})
    LIST(APPEND OPENCV_MODULE_NAMES ${OPENCV_MODULE_NAME})  
  ENDIF() 
ENDFOREACH()

#IF(NETFX_CORE)
#  SET(EMGU_CV_WITH_TESSERACT OFF CACHE BOOL "BUILD TESSERACT OCR")
#ELSE()
SET(EMGU_CV_WITH_TESSERACT ON CACHE BOOL "BUILD TESSERACT OCR")
#ENDIF()

IF(MSVC AND WIN32 AND EMGUCV_WITH_TESSERACT)
  SET(TESSERACT_OPENCL OFF CACHE BOOL "ENABLE OpenCL for TESSERACT")
ELSE()
  SET(TESSERACT_OPENCL OFF CACHE BOOL "ENABLE OpenCL for TESSERACT")
ENDIF()

IF(TESSERACT_OPENCL)
  ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/lib/opencl-icd-1.2.11.0/icd)
ENDIF()

IF (TARGET_ARCH_64)
  SET(EMGU_ENABLE_SSE OFF CACHE BOOL "ENABLE SSE for Emgu CV")
ELSE()
  SET(EMGU_ENABLE_SSE ON CACHE BOOL "ENABLE SSE for Emgu CV")
ENDIF()
ADD_SUBDIRECTORY(Emgu.CV.Extern)
ADD_SUBDIRECTORY(Emgu.CV.NativeImage)

IF(NOT (ANDROID OR IOS OR NETFX_CORE))
  ENABLE_TESTING()
  ADD_SUBDIRECTORY(tests/cvextern_test)
ENDIF()

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}" )
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX_COPY})
#SET(CPACK_PACKAGING_INSTALL_PREFIX "")
#SET(DESTDIR "")
#SET(CPACK_OUTPUT_FILE_PREFIX "")

SET(EMGU_CV_BUILD ON CACHE BOOL "BUILD Emgu CV")
SET(EMGU_CV_EXAMPLE_BUILD ON CACHE BOOL "Build Emgu CV Example")

# ----------------------------------------------------------------------------
#  Generate required common assembly file for Emgu CV project
# ----------------------------------------------------------------------------
INCLUDE(Today)
TODAY() #set the variable CURRENT_YEAR
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CommonAssemblyInfo.cs.in ${CMAKE_CURRENT_SOURCE_DIR}/CommonAssemblyInfo.cs)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.Extern/version.rc)

MACRO(CHECK_LICENSE_TYPE)
	MESSAGE(STATUS "LICENSE FILE: ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.License.txt")
    IF (WIN32)
        EXECUTE_PROCESS(COMMAND "findstr" "gpl" "Emgu.CV.License.txt" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE TMP)
	ELSE()	
	    EXECUTE_PROCESS(COMMAND "grep" "gpl" "Emgu.CV.License.txt" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE TMP)
	ENDIF()
	
	STRING(STRIP "${TMP}" TMP)
	STRING(LENGTH "${TMP}" GPL_LICENSE_LENGTH)
	SET (LICENSE_TYPE "OpenSource")
	SET (EMGUCV_LICENSE_FILE "Emgu.CV.License.txt")
	#SET (EMGUCV_LICENSE_URL "https://www.gnu.org/licenses/gpl-3.0.txt")
	IF(${GPL_LICENSE_LENGTH} EQUAL 0)
		SET(LICENSE_TYPE "Commercial")
		#SET (EMGUCV_LICENSE_URL "http://www.emgu.com/wiki/files/Emgu.CV.Commercial.License.4.0.pdf")
	ENDIF()
	
	MESSAGE(STATUS "LICENSE_TYPE: ${LICENSE_TYPE}")
ENDMACRO()
CHECK_LICENSE_TYPE()

# ----------------------------------------------------------------------------
#  Generate neget packaging file for Emgu CV project
# ----------------------------------------------------------------------------
SET(EMGUCV_NUGET_ID "Emgu.CV")
SET(EMGUCV_WINDOWS_NUGET_ID "${EMGUCV_NUGET_ID}.runtime.windows")
#SET(
IF(WITH_CUDA)
	SET(EMGUCV_WINDOWS_NUGET_ID "${EMGUCV_WINDOWS_NUGET_ID}.cuda")
	#SET(EMGUCV_NUGET_ID "${EMGUCV_NUGET_ID_BASE}-CUDA")
	#SET(EMGU_CUDA_NUGET_ID "${EMGUCV_NUGET_ID}.Native")
	SET(EMGU_NPP_NUGET_ID "${EMGUCV_WINDOWS_NUGET_ID}.npp")
	SET(EMGU_CUFFT_NUGET_ID "${EMGUCV_WINDOWS_NUGET_ID}.cufft")
	SET(EMGU_CUDNN_NUGET_ID "${EMGUCV_WINDOWS_NUGET_ID}.dnn")
#ELSE()
#	SET(EMGUCV_NUGET_ID "${EMGUCV_NUGET_ID_BASE}")
ENDIF()

IF(HAVE_OSX)
  SET(EMGUCV_MACOS_NUGET_ID "${EMGUCV_NUGET_ID}.runtime.macos")
ENDIF()

IF(HAVE_ANDROID_ALL_ARCH AND WIN32)
	SET(EMGUCV_ANDROID_NUGET_ID "${EMGUCV_NUGET_ID}.runtime.android")
ENDIF()

IF(HAVE_IOS AND WIN32)
	SET(EMGUCV_IOS_NUGET_ID "${EMGUCV_NUGET_ID}.runtime.ios")
ENDIF()

IF(HAVE_WINSTORE_10 AND WIN32)
	SET(EMGUCV_UWP_NUGET_ID "${EMGUCV_NUGET_ID}.runtime.uwp")
ENDIF()

IF ("${LICENSE_TYPE}" STREQUAL "Commercial")
	MESSAGE(STATUS "Configuring nuget package for Commercial release")
	SET(EMGUCV_NUGET_ID "${EMGUCV_NUGET_ID}-CR")
	IF(HAVE_OSX)
	  SET(EMGUCV_MACOS_NUGET_ID "${EMGUCV_MACOS_NUGET_ID}-CR")
	ENDIF()
	IF(HAVE_ANDROID_ALL_ARCH AND WIN32)
	  SET(EMGUCV_ANDROID_NUGET_ID "${EMGUCV_ANDROID_NUGET_ID}-CR")
	ENDIF()
	IF(HAVE_IOS AND WIN32)
	  SET(EMGUCV_IOS_NUGET_ID "${EMGUCV_IOS_NUGET_ID}-CR")
	ENDIF()
	IF(HAVE_WINSTORE_10 AND WIN32)
	  SET(EMGUCV_UWP_NUGET_ID "${EMGUCV_UWP_NUGET_ID}-CR")
	ENDIF()
	IF(HAVE_WINDESKTOP_X64 OR HAVE_WINDESKTOP_X86)
	  SET(EMGUCV_WINDOWS_NUGET_ID "${EMGUCV_WINDOWS_NUGET_ID}-CR")
	ENDIF()
ELSEIF()
	MESSAGE(STATUS "Configuring nuget package for Open Source release")
ENDIF()


SET(EMGUCV_WINDOWS_NUGET_FILE_LIST "")
IF(HAVE_WINDESKTOP_X86)
	SET(EMGUCV_WINDOWS_NUGET_FILE_LIST "${EMGUCV_WINDOWS_NUGET_FILE_LIST} <file src=\"..\\..\\..\\libs\\x86\\*.dll\" exclude=\"..\\..\\..\\libs\\x86\\cu*.dll;..\\..\\..\\libs\\x86\\npp*.dll\" target=\"build\\x86\" />")
ENDIF()
IF(HAVE_WINDESKTOP_X64)
	SET(EMGUCV_WINDOWS_NUGET_FILE_LIST "${EMGUCV_WINDOWS_NUGET_FILE_LIST} <file src=\"..\\..\\..\\libs\\x64\\*.dll\" exclude=\"..\\..\\..\\libs\\x64\\cu*.dll;..\\..\\..\\libs\\x64\\npp*.dll\" target=\"build\\x64\" />")
ENDIF()
IF(HAVE_WINDESKTOP_X86 OR HAVE_WINDESKTOP_X64)
	SET(EMGUCV_WINDOWS_NUGET_FILE_LIST "${EMGUCV_WINDOWS_NUGET_FILE_LIST} 
	<file src=\"Emgu.CV.runtime.windows.targets\" target=\"build\\${EMGUCV_WINDOWS_NUGET_ID}.targets\" />
	<file src=\"..\\..\\..\\libs\\Emgu.CV.UI.dll\" target=\"lib\\net461\" />
	<file src=\"..\\..\\..\\libs\\Emgu.CV.UI.xml\" target=\"lib\\net461\" />
	<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
	<file src=\"..\\..\\..\\${EMGUCV_LICENSE_FILE}\" target=\"\" />")
ENDIF()

SET(EMGUCV_MACOS_NUGET_FILE_LIST "")
IF(HAVE_OSX)
	SET(EMGUCV_MACOS_NUGET_FILE_LIST "${EMGUCV_MACOS_NUGET_FILE_LIST} 
	<file src=\"Emgu.CV.runtime.macos.targets\" target=\"build\\${EMGUCV_MACOS_NUGET_ID}.targets\" />
	<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
	<file src=\"..\\..\\..\\${EMGUCV_LICENSE_FILE}\" target=\"\" />")
	SET(EMGUCV_MACOS_NUGET_DEPENDENCY "
	<dependencies>
		<dependency id=\"${EMGUCV_NUGET_ID}\" version=\"[${CPACK_PACKAGE_VERSION}]\" />
	</dependencies>")
ENDIF()

SET(EMGUCV_UWP_NUGET_FILE_LIST "")
IF(HAVE_WINSTORE_10_X86)
	SET(EMGUCV_UWP_NUGET_FILE_LIST "${EMGUCV_UWP_NUGET_FILE_LIST} <file src=\"..\\..\\..\\libs\\UWP\\x86\\*.dll\" target=\"build\\UWP\\x86\" />")
ENDIF()
IF(HAVE_WINSTORE_10_X64)
	SET(EMGUCV_UWP_NUGET_FILE_LIST "${EMGUCV_UWP_NUGET_FILE_LIST} <file src=\"..\\..\\..\\libs\\UWP\\x64\\*.dll\" target=\"build\\UWP\\x64\" />")
ENDIF()
IF(HAVE_WINSTORE_10_ARM)
	SET(EMGUCV_UWP_NUGET_FILE_LIST "${EMGUCV_UWP_NUGET_FILE_LIST} <file src=\"..\\..\\..\\libs\\UWP\\arm\\*.dll\" target=\"build\\UWP\\arm\" />")
ENDIF()
IF(HAVE_WINSTORE_10_ARM64)
	SET(EMGUCV_UWP_NUGET_FILE_LIST "${EMGUCV_UWP_NUGET_FILE_LIST} <file src=\"..\\..\\..\\libs\\UWP\\arm64\\*.dll\" target=\"build\\UWP\\arm64\" />")
ENDIF()
IF(HAVE_WINSTORE_10)
    SET(EMGUCV_UWP_NUGET_DEPENDENCY "
	<dependencies>
		<dependency id=\"${EMGUCV_NUGET_ID}\" version=\"[${CPACK_PACKAGE_VERSION}]\" />
	</dependencies>")
	SET(EMGUCV_UWP_NUGET_FILE_LIST "${EMGUCV_UWP_NUGET_FILE_LIST} 
	<file src=\"Emgu.CV.runtime.uwp.targets\" target=\"build\\${EMGUCV_UWP_NUGET_ID}.targets\" />
	<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
	<file src=\"..\\..\\..\\${EMGUCV_LICENSE_FILE}\" target=\"\" />")
ENDIF()


SET(EMGUCV_NUGET_FILE_LIST "")
SET(EMGUCV_NUGET_FILE_LIST "${EMGUCV_NUGET_FILE_LIST} 
	<file src=\"..\\..\\..\\libs\\Emgu.CV.World.NetStandard.dll\" target=\"lib\\netstandard2.0\" />
	<file src=\"..\\..\\..\\libs\\Emgu.CV.World.NetStandard.xml\" target=\"lib\\netstandard2.0\" />
	<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
	<file src=\"..\\..\\..\\${EMGUCV_LICENSE_FILE}\" target=\"\" />")

SET(EMGUCV_NUGET_TITLE_ARCH "")
IF(HAVE_WINDESKTOP_X86)
 SET(EMGUCV_NUGET_TITLE_ARCH "${EMGUCV_NUGET_TITLE_ARCH} x86")
ENDIF()
IF(HAVE_WINDESKTOP_X64)
 SET(EMGUCV_NUGET_TITLE_ARCH "${EMGUCV_NUGET_TITLE_ARCH} x64")
ENDIF()
STRING(STRIP "${EMGUCV_NUGET_TITLE_ARCH}" EMGUCV_NUGET_TITLE_ARCH)
STRING(REPLACE "x86 x64" "" EMGUCV_NUGET_TITLE_ARCH "${EMGUCV_NUGET_TITLE_ARCH}")
 
SET(EMGUCV_WINDOWS_NUGET_TITLE "EMGU CV ${EMGUCV_NUGET_TITLE_ARCH}")
#SET(EMGUCV_WINDOWS_NUGET_TITLE "EMGU CV ${EMGUCV_NUGET_TITLE_ARCH}")
STRING(STRIP "${EMGUCV_WINDOWS_NUGET_TITLE}" EMGUCV_WINDOWS_NUGET_TITLE)

SET(EMGUCV_WINDOWS_NET461_NUGET_DEPENDENCY "<dependency id=\"ZedGraph\" version=\"[5.1.7]\" />")
SET(EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY "<dependency id=\"${EMGUCV_NUGET_ID}\" version=\"[${CPACK_PACKAGE_VERSION}]\" />")
#SET(EMGUCV_WINDOWS_NUGET_DEPENDENCY "<group targetFramework=\"net461\"><dependency id=\"ZedGraph\" version=\"[5.1.7]\" /></group>")


IF(WITH_CUDA)
	SET(EMGUCV_WINDOWS_NUGET_TITLE "${EMGUCV_WINDOWS_NUGET_TITLE} with CUDA support")
	SET(EMGU_CUDA_NUGET_TITLE "Native x64 CUDA dependency for EMGU CV")
	SET(EMGU_CUDA_NPP_NUGET_FILE_LIST "<file src=\"${EMGU_NPP_NUGET_ID}.targets\" target=\"build\\${EMGU_NPP_NUGET_ID}.targets\" />")
	SET(EMGU_CUDA_VERSION "${npp_major}.${npp_minor}.${npp_build}")
	IF ( (${cuda_major}) AND (${cuda_minor}) )
		SET(EMGU_CUDA_VERSION "${cuda_major}.${cuda_minor}.${npp_build}")
	ENDIF()
	SET(EMGU_CUFFT_VERSION "${EMGU_CUDA_VERSION}")
	SET(EMGU_CUFFT_NUGET_TITLE "Native x64 CUDA CUFFT dependency for EMGU CV")
	SET(EMGU_CUFFT_NUGET_FILE_LIST "<file src=\"${EMGU_CUFFT_NUGET_ID}.targets\" target=\"build\\${EMGU_CUFFT_NUGET_ID}.targets\" />")
    SET(EMGU_CUDNN_VERSION "${CUDNN_MAJOR_VERSION}.${CUDNN_MINOR_VERSION}.${CUDNN_PATCH_VERSION}")
	SET(EMGU_CUDNN_NUGET_TITLE "Native x64 CUDA DNN dependency for EMGU CV")
	SET(EMGU_CUDNN_NUGET_FILE_LIST "<file src=\"${EMGU_CUDNN_NUGET_ID}.targets\" target=\"build\\${EMGU_CUDNN_NUGET_ID}.targets\" />")
	SET(EMGU_NPP_VERSION "${EMGU_CUDA_VERSION}")
	SET(EMGU_NPP_NUGET_TITLE "Native x64 CUDA dependency for EMGU CV")
	SET(EMGU_NPP_NUGET_FILE_LIST "<file src=\"${EMGU_NPP_NUGET_ID}.targets\" target=\"build\\${EMGU_NPP_NUGET_ID}.targets\" />")
	#SET(EMGUCV_NUGET_DEPENDENCY "${EMGUCV_NUGET_DEPENDENCY} <dependency id=\"${EMGUCV_WINDOWS_NUGET_ID}\" version=\"[${EMGU_CUDA_VERSION}]\" />")
	IF (WITH_CUDNN)
		SET(EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY "${EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY}<dependency id=\"${EMGU_CUDNN_NUGET_ID}\" version=\"[${EMGU_CUDNN_VERSION}]\" />")
	ENDIF()
	IF (WITH_CUDA)
	  SET(EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY "${EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY}<dependency id=\"${EMGU_CUFFT_NUGET_ID}\" version=\"[${EMGU_CUFFT_VERSION}]\" />")
	  SET(EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY "${EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY}<dependency id=\"${EMGU_NPP_NUGET_ID}\" version=\"[${EMGU_NPP_VERSION}]\" />")
	ENDIF()
	IF(HAVE_WINDESKTOP_X64)
		file(COPY "${CUDA_TOOLKIT_ROOT_DIR}/EULA.txt" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.windows.cuda.npp")
		file(COPY "${CUDA_TOOLKIT_ROOT_DIR}/EULA.txt" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.windows.cuda.cufft")
		#file(COPY "${CUDA_TOOLKIT_ROOT_DIR}/EULA.txt" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Cudnn")
		SET(EMGU_CUDA_NPP_NUGET_FILE_LIST "${EMGU_CUDA_NPP_NUGET_FILE_LIST} 
		<file src=\"..\\..\\..\\libs\\x64\\cu*.dll\" exclude=\"..\\..\\..\\libs\\x64\\cufft*.dll;..\\..\\..\\libs\\x64\\cudnn*.dll\" target=\"build\\x64\" /> 
		<file src=\"..\\..\\..\\libs\\x64\\npp*.dll\" target=\"build\\x64\" />
		<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />		
		<file src=\"EULA.txt\" target=\"CUDA_EULA.txt\" />")
		SET(EMGU_CUFFT_NUGET_FILE_LIST "${EMGU_CUFFT_NUGET_FILE_LIST} 
		<file src=\"..\\..\\..\\libs\\x64\\cufft*.dll\" target=\"build\\x64\" />
		<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
		<file src=\"EULA.txt\" target=\"CUDA_EULA.txt\" />")
		SET(EMGU_CUDNN_NUGET_FILE_LIST "${EMGU_CUDNN_NUGET_FILE_LIST} 
		<file src=\"..\\..\\..\\libs\\x64\\cudnn*.dll\" target=\"build\\x64\" />
		<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
		<file src=\"NVIDIA_SLA_cuDNN_Support.txt\" target=\"cuDNN_SLA.txt\" />")
	ENDIF()
	
	SET(NUGET_PACKAGE_VENDOR "${CPACK_PACKAGE_VENDOR}")
	
	#MESSAGE(STATUS "<<<<<<<<<<<<<<<< npp_major: ${npp_major} >>>>>>>>>>>>>>>>>>>>>> ")
	#MESSAGE(STATUS "<<<<<<<<<<<<<<<< npp_minor: ${npp_minor} >>>>>>>>>>>>>>>>>>>>>> ")
	#MESSAGE(STATUS "<<<<<<<<<<<<<<<< npp_build: ${npp_build} >>>>>>>>>>>>>>>>>>>>>> ")
	
	
	IF ( (${cuda_major}) AND (${cuda_minor}) )
	  SET(CUDA_VERSION_NUMERIC_ONLY "${cuda_major}${cuda_minor}")
	ELSE()
	  SET(CUDA_VERSION_NUMERIC_ONLY "${npp_major}${npp_minor}")
	ENDIF()
	
	IF((${npp_major} GREATER 10) OR (("${npp_major}" STREQUAL "10") AND (${npp_minor} GREATER 0)))
		IF (${cuda_major})
		  SET(CUFFT_POSTFIX "${cuda_major}")
		else()
		  SET(CUFFT_POSTFIX "${npp_major}")
		endif()
	ELSE()
		SET(CUFFT_POSTFIX "${npp_major}${npp_minor}")
	ENDIF()
	
	#MESSAGE(STATUS "<<<<<<<<<<<<<<<< CUDA_VERSION_NUMERIC_ONLY: ${CUDA_VERSION_NUMERIC_ONLY} >>>>>>>>>>>>>>>>>>>>>> ")
	
	#CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.windows.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.windows/Package.nuspec)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EMGU_CUFFT_NUGET_ID}.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/${EMGU_CUFFT_NUGET_ID}/Package.nuspec)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EMGU_CUDNN_NUGET_ID}.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/${EMGU_CUDNN_NUGET_ID}/Package.nuspec)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EMGU_NPP_NUGET_ID}.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/${EMGU_NPP_NUGET_ID}/Package.nuspec)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EMGU_CUFFT_NUGET_ID}.targets.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/${EMGU_CUFFT_NUGET_ID}/${EMGU_CUFFT_NUGET_ID}.targets)
	#CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.windows.targets.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.windows/${EMGUCV_WINDOWS_NUGET_ID}.targets)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EMGU_CUDNN_NUGET_ID}.targets.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/${EMGU_CUDNN_NUGET_ID}/${EMGU_CUDNN_NUGET_ID}.targets)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EMGU_NPP_NUGET_ID}.targets.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/${EMGU_NPP_NUGET_ID}/${EMGU_NPP_NUGET_ID}.targets)
ENDIF()

SET(EMGUCV_WINDOWS_NUGET_DEPENDENCY "	<dependencies>
      <group targetFramework=\"net461\">${EMGUCV_WINDOWS_NET461_NUGET_DEPENDENCY}${EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY}</group>
	  <group targetFramework=\"netstandard2.0\">${EMGUCV_WINDOWS_NETSTANDARD_NUGET_DEPENDENCY}</group>
	</dependencies>")

IF(HAVE_ANDROID_ALL_ARCH AND WIN32)
	SET(EMGUCV_ANDROID_NUGET_TITLE "Emgu CV for Android Native Runtime")	
	SET(EMGUCV_ANDROID_NUGET_FILE_LIST " 
    <file src=\"..\\..\\..\\libs\\Emgu.CV.Runtime.Android.dll\" target=\"lib\\MonoAndroid50\" />
	<file src=\"..\\..\\..\\libs\\Emgu.CV.Runtime.Android.xml\" target=\"lib\\MonoAndroid50\" />")
	SET(EMGUCV_ANDROID_NUGET_FILE_LIST "${EMGUCV_ANDROID_NUGET_FILE_LIST} 
	<file src=\"Emgu.CV.runtime.android.targets\" target=\"build\\${EMGUCV_ANDROID_NUGET_ID}.targets\" />
	<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
	<file src=\"..\\..\\..\\${LICENSE_FILE_NAME}\" target=\"\" />")
	SET(EMGUCV_ANDROID_NUGET_DEPENDENCY "
	<dependencies>
		<dependency id=\"${EMGUCV_NUGET_ID}\" version=\"[${CPACK_PACKAGE_VERSION}]\" />
	</dependencies>")
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.android.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.android/Package.nuspec)
ENDIF()

IF(HAVE_IOS AND WIN32)
	SET(EMGUCV_IOS_NUGET_TITLE "Emgu CV for iOS")	
	SET(EMGUCV_IOS_NUGET_FILE_LIST " 
    <file src=\"..\\..\\..\\libs\\Emgu.CV.World.IOS.dll\" target=\"lib\\Xamarin.iOS10\" />
	<file src=\"..\\..\\..\\libs\\Emgu.CV.World.IOS.XML\" target=\"lib\\Xamarin.iOS10\" />")
	SET(EMGUCV_IOS_NUGET_FILE_LIST "${EMGUCV_IOS_NUGET_FILE_LIST} 
	<file src=\"Emgu.CV.runtime.ios.targets\" target=\"build\\${EMGUCV_IOS_NUGET_ID}.targets\" />
	<file src=\"..\\..\\..\\miscellaneous\\nuget_icon.png\" target=\"icon.png\" />
	<file src=\"..\\..\\..\\${LICENSE_FILE_NAME}\" target=\"\" />")
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.ios.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.ios/Package.nuspec)
ENDIF()

IF(HAVE_OSX)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.macos.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.macos/Package.nuspec)
ENDIF()

IF(HAVE_WINSTORE_10)
  SET(EMGUCV_UWP_NUGET_TITLE "Emgu CV for UWP Native Runtime")
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.uwp.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.uwp/Package.nuspec)
ENDIF()

IF(HAVE_WINDESKTOP_X86 OR HAVE_WINDESKTOP_X64)
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.runtime.windows.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV.runtime.windows/Package.nuspec)
ENDIF()

SET(EMGUCV_NUGET_SUMMARY "Emgu CV is a cross platform .Net wrapper to the OpenCV image processing library.")

IF ("${LICENSE_TYPE}" STREQUAL "Commercial")
	SET(EMGUCV_NUGET_TITLE "${EMGUCV_NUGET_TITLE} Commercial Release")
	SET(EMGUCV_NUGET_SUMMARY "${EMGUCV_NUGET_SUMMARY} This is the commercial release.")
	IF(HAVE_ANDROID_ALL_ARCH AND WIN32)
	  SET(EMGUCV_ANDROID_NUGET_TITLE "${EMGUCV_ANDROID_NUGET_TITLE} Commercial Release")	
	ENDIF()	
	IF(HAVE_WINSTORE_10)
	  SET(EMGUCV_UWP_NUGET_TITLE "${EMGUCV_UWP_NUGET_TITLE} Commercial Release")
	ENDIF()
ENDIF()

#generate nuget config file for Emgu.CV 
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Emgu.CV.Package.nuspec.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/nuget/Emgu.CV/Package.nuspec)

#CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/component.yaml.in ${CMAKE_CURRENT_SOURCE_DIR}/platforms/xamarin/emgucv_v3/component/component.yaml)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Example.Android.AndroidManifest.xml.in ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.Example/Android/Properties/AndroidManifest.xml @ONLY)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/XamarinForms.Android.AndroidManifest.xml.in ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.Example/XamarinForms/Android/Properties/AndroidManifest.xml @ONLY)

# ----------------------------------------------------------------------------
#  Generate readme file for Emgu CV project
# ----------------------------------------------------------------------------
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/README.txt.in ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV/README.txt)
INSTALL(FILES "${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV/README.txt" DESTINATION "." COMPONENT libs)

# ----------------------------------------------------------------------------
#  Generate CvInvokeEntryPoints.cs file for Emgu CV project
# ----------------------------------------------------------------------------
SET(OPENCV_LIBRARY_OUTPUT_NAME_PREFIX "")
IF (APPLE)
  SET(OPENCV_LIBRARY_OUTPUT_NAME_POSTFIX ".${OPENCV_VERSION_MAJOR}.${OPENCV_VERSION_MINOR}.${OPENCV_VERSION_PATCH}")
ELSE()
  SET(OPENCV_LIBRARY_OUTPUT_NAME_POSTFIX "")
ENDIF()

SET(OPENCV_LIBRARY_OUTPUT_NAMES )

FOREACH(OPENCV_MODULE_NAME ${OPENCV_MODULE_NAME_CANDIDATES})
  STRING (TOUPPER "${OPENCV_MODULE_NAME}" OPENCV_MODULE_NAME_UPPER)
  
  STRING (SUBSTRING "${OPENCV_MODULE_NAME}" 0 1 OPENCV_MODULE_NAME_FIRST_LETTER)
  STRING (TOUPPER "${OPENCV_MODULE_NAME_FIRST_LETTER}" OPENCV_MODULE_NAME_FIRST_LETTER_UPPER)
  STRING (SUBSTRING "${OPENCV_MODULE_NAME}" 1 -1 OPENCV_MODULE_NAME_REMAINDER)
  SET(OPENCV_MODULE_NAME_FIRST_LETTER_UPPER "${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}${OPENCV_MODULE_NAME_REMAINDER}")
  
  SET(OPENCV_OUTPUT_PROJ_TYPE "")
  IF(NOT (TARGET opencv_${OPENCV_MODULE_NAME}))
    SET(OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME "")
  ELSE()
    #MESSAGE( STATUS "opencv_${OPENCV_MODULE_NAME} type: ${OPENCV_OUTPUT_PROJ_TYPE}")
    GET_TARGET_PROPERTY(OPENCV_OUTPUT_PROJ_TYPE opencv_${OPENCV_MODULE_NAME} TYPE)
    IF(NOT ("${OPENCV_OUTPUT_PROJ_TYPE}" STREQUAL "SHARED_LIBRARY"))
      SET(OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME "")
    ELSE()
      GET_TARGET_PROPERTY(OPENCV_OUTPUT_NAME_TMP opencv_${OPENCV_MODULE_NAME} OUTPUT_NAME)
      SET(OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME  ${OPENCV_LIBRARY_OUTPUT_NAME_PREFIX}${OPENCV_OUTPUT_NAME_TMP}${OPENCV_LIBRARY_OUTPUT_NAME_POSTFIX})
      LIST(APPEND OPENCV_LIBRARY_OUTPUT_NAMES ${OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME})
      SET(ENTRYPOINT_MODULES_STRING "${ENTRYPOINT_MODULES_STRING}
      Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library,")
    ENDIF()
  ENDIF()
  
  IF(IOS)
    SET(ENTRYPOINT_CS_STRING "${ENTRYPOINT_CS_STRING} 
      /// <summary>
      /// The file name of the ${OPENCV_MODULE_NAME} library
      /// </summary>
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"__Internal\";")
  ELSEIF(((WIN32 AND NOT ANDROID) OR APPLE))
    IF(("${OPENCV_OUTPUT_PROJ_TYPE}" STREQUAL "SHARED_LIBRARY"))
      SET(ANDROID_ENTRYPOINT "opencv_${OPENCV_MODULE_NAME}")
      SET(OSX_ENTRYPOINT "opencv_${OPENCV_MODULE_NAME}.${OPENCV_VERSION_MAJOR}.${OPENCV_VERSION_MINOR}.${OPENCV_VERSION_PATCH}")
      SET(OSX_UNITY_STANDALONE_ENTRYPOINT "@executable_path/../Plugins/emgucv.bundle/Contents/MacOS/lib${OSX_ENTRYPOINT}.dylib")
      SET(OSX_UNITY_EDITOR_ENTRYPOINT "Assets/Plugins/emgucv.bundle/Contents/MacOS/lib${OSX_ENTRYPOINT}.dylib")
      SET(WINDOWS_ENTRYPOINT "opencv_${OPENCV_MODULE_NAME}${OPENCV_VERSION_MAJOR}${OPENCV_VERSION_MINOR}${OPENCV_VERSION_PATCH}")
      SET(IOS_ENTRYPOINT "__Internal")
      
      IF("${OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME}" STREQUAL "")
        SET(ANDROID_ENTRYPOINT "")
        SET(OSX_ENTRYPOINT "")
        SET(OSX_UNITY_ENTRYPOINT "emgucv")
        SET(WINDOWS_ENTRYPOINT "")
        SET(IOS_ENTRYPOINT "")
      ENDIF()
      
      SET(ENTRYPOINT_CS_STRING "${ENTRYPOINT_CS_STRING} 
      /// <summary>
      /// The file name of the ${OPENCV_MODULE_NAME} library
      /// </summary>
      #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${WINDOWS_ENTRYPOINT}\";
      #elif UNITY_EDITOR_OSX
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${OSX_UNITY_EDITOR_ENTRYPOINT}\";   
      #elif UNITY_STANDALONE_OSX
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${OSX_UNITY_STANDALONE_ENTRYPOINT}\";   
      #elif __IOS__ || UNITY_IPHONE
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${IOS_ENTRYPOINT}\";
      #elif __ANDROID__ || UNITY_ANDROID
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${ANDROID_ENTRYPOINT}\";
      #else
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME}\";
      #endif
")
    ENDIF()
  ELSE()
    IF("${OPENCV_OUTPUT_PROJ_TYPE}" STREQUAL "SHARED_LIBRARY")
      SET(ENTRYPOINT_CS_STRING "${ENTRYPOINT_CS_STRING} 
      /// <summary>
      /// The file name of the ${OPENCV_MODULE_NAME} library
      /// </summary>
      public const string Opencv${OPENCV_MODULE_NAME_FIRST_LETTER_UPPER}Library = \"${OPENCV_${OPENCV_MODULE_NAME_UPPER}_OUTPUT_NAME}\";")
    ENDIF()
  ENDIF()
  
ENDFOREACH()

SET(ENTRYPOINT_MODULES_STRING "public static List<String> OpenCVModuleList = new List<String>
	  {
#if !(__ANDROID__ || __IOS__ || UNITY_IPHONE || UNITY_ANDROID || NETFX_CORE)
        OpencvFFMpegLibrary,
#endif        
        ${ENTRYPOINT_MODULES_STRING}
        ExternLibrary
      };
")

GET_TARGET_PROPERTY(CVEXTERN_OUTPUT_NAME cvextern OUTPUT_NAME)
LIST(APPEND OPENCV_LIBRARY_OUTPUT_NAMES ${CVEXTERN_OUTPUT_NAME})

if(WIN32 AND WITH_FFMPEG)
  if(TARGET_ARCH_64)
    set(FFMPEG_SUFFIX _64)
  endif()
  #STRING(LENGTH "${OPENCV_CORE_OUTPUT_NAME}" temp)
  #MATH(EXPR temp "${temp}-3")
  #STRING(SUBSTRING ${OPENCV_CORE_OUTPUT_NAME} ${temp} -1 OPENCV_DLLVERSION)
  set(OPENCV_FFMPEG_OUTPUT_NAME "opencv_videoio_ffmpeg${OPENCV_VERSION_MAJOR}${OPENCV_VERSION_MINOR}${OPENCV_VERSION_PATCH}${FFMPEG_SUFFIX}")
  #set(OPENCV_FFMPEG_OUTPUT_NAME "opencv_ffmpeg${FFMPEG_SUFFIX}")
  MESSAGE(STATUS "OPENCV_FFMPEG_OUTPUT_NAME: ${OPENCV_FFMPEG_OUTPUT_NAME}")
  LIST(APPEND OPENCV_LIBRARY_OUTPUT_NAMES ${OPENCV_FFMPEG_OUTPUT_NAME})
endif()

IF(TARGET_ARCH_64)
  SET(UNMANAGED_CODE_BITNESS "64")
ELSE()
  SET(UNMANAGED_CODE_BITNESS "32")
ENDIF()

#IF(ANDROID)
#  SET(CVINVOKE_ENTRYPOINTS_FILE_SUBFOLDER "Android/")
#ELSE()
SET(CVINVOKE_ENTRYPOINTS_FILE_SUBFOLDER "")
#ENDIF()

IF(IOS)
  SET(CVEXTERN_OUTPUT_NAME "__Internal")
ENDIF()
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CvInvokeEntryPoints.cs.in ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV/PInvoke/${CVINVOKE_ENTRYPOINTS_FILE_SUBFOLDER}CvInvokeEntryPoints.cs)

# ----------------------------------------------------------------------------
#  Build the managed dlls of Emgu CV
# ----------------------------------------------------------------------------
#IF(NOT IOS) 
  ADD_SUBDIRECTORY(Emgu.Util)
  ADD_SUBDIRECTORY(Emgu.CV)
  ADD_SUBDIRECTORY(Emgu.CV.UI)
  ADD_SUBDIRECTORY(Emgu.CV.Example)
  #ADD_SUBDIRECTORY(Emgu.UI)
  ADD_SUBDIRECTORY(Emgu.CV.Test)
  ADD_SUBDIRECTORY(Emgu.CV.Cuda)
  IF (EMGU_CV_WITH_TESSERACT)
    ADD_SUBDIRECTORY(Emgu.CV.OCR)
  ENDIF()
    
  ADD_SUBDIRECTORY(Emgu.CV.Contrib)
#  IF(NOT (NETFX_CORE OR ANDROID))
#    ADD_SUBDIRECTORY(Emgu.CV.UI.GL)
#  ENDIF()
  ADD_SUBDIRECTORY(Emgu.CV.World)
#ENDIF()
ADD_SUBDIRECTORY(Emgu.CV.Runtime)
#IF(WIN32)
  ADD_SUBDIRECTORY(platforms/nuget)
#ENDIF()

IF(HAVE_ANDROID)
  INSTALL(
    FILES
    "Solution/Android/Emgu.CV.Android.Example.sln"
    "Solution/Android/Emgu.CV.Android.sln"
    DESTINATION "Solution/Android"
    COMPONENT emgucv_example_source
    )
  INSTALL(
    FILES
    "opencv/LICENSE"
    DESTINATION "opencv"
    COMPONENT emgucv_example_source
    )    
ENDIF()

IF(HAVE_IOS)
  INSTALL(
    FILES
    "Solution/iOS/Emgu.CV.iOS.Example.sln"
    "Solution/iOS/Emgu.CV.iOS.sln"
    DESTINATION "Solution/iOS"
    COMPONENT emgucv_example_source
    )
ENDIF()

IF(HAVE_OSX)
  INSTALL(
    FILES
    "Solution/Mac/Emgu.CV.Mac.Example.sln"
    DESTINATION "Solution/Mac"
    COMPONENT emgucv_example_source
    )
  IF (NOT IS_OSX_BUILD)
    INSTALL(
      FILES
      "libs/osx/libcvextern.dylib"
      DESTINATION "libs/osx"
      COMPONENT emgucv_binary
      )
  ENDIF()
ENDIF()

INSTALL(
  FILES
  "${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.snk"
  "${CMAKE_CURRENT_SOURCE_DIR}/CommonAssemblyInfo.cs"
  "${CMAKE_CURRENT_SOURCE_DIR}/${LICENSE_FILE_NAME}"
  "${CMAKE_CURRENT_SOURCE_DIR}/nuget.config"
  "${CMAKE_CURRENT_SOURCE_DIR}/components.config"
  DESTINATION .
  COMPONENT emgucv_source
  )
  
IF (EMGU_CV_WITH_FREETYPE)
  IF (ANDROID)  
	#INSTALL(EXPORT freetype DESTINATION lib)
  ENDIF()
ENDIF()

IF (ANDROID)
  SET(CPACK_GENERATOR ZIP)
ELSEIF (WIN32)
  IF(NETFX_CORE)
    SET(CPACK_GENERATOR ZIP)
  ELSE()
    #SET(CPACK_GENERATOR ZIP)
    SET(CPACK_GENERATOR NSIS;ZIP)
  ENDIF()
  
  SET(CPACK_ARCHIVE_COMPONENT_INSTALL ON) #enable components install for zip
  IF(CPACK_GENERATOR MATCHES "ZIP")
    SET(CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE 1)
  ENDIF()
  SET(CPACK_NSIS_MODIFY_PATH OFF)
  SET(CPACK_NSIS_INSTALL_ROOT "C:\\\\Emgu")
  
  IF(NOT NETFX_CORE)
    ADD_SUBDIRECTORY(Emgu.CV.DebuggerVisualizers)
  ENDIF()
   
  IF(NOT NETFX_CORE)
    
    if(WITH_FFMPEG)
      
      INSTALL(
	FILES
	"${UNMANAGED_LIBRARY_OUTPUT_PATH}/${OPENCV_FFMPEG_OUTPUT_NAME}.dll"
	DESTINATION "libs/${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER}/"
	COMPONENT libs
	)
    endif()
    
    # ----------------------------------------------------------------------------
    #  Install Debugger Visualizer
    # ----------------------------------------------------------------------------
    
    SET (HAS_WINDESKTOP_X86_NATIVE_BINARY OFF)
    IF (IS_WINDESKTOP_X86_BUILD)
      FOREACH(tmp_component ${OPENCV_LIBRARY_OUTPUT_NAMES})
	LIST(APPEND UNMANAGED_DLL_32BIT "${UNMANAGED_LIBRARY_OUTPUT_PATH}/${tmp_component}.dll")
      ENDFOREACH()
      FOREACH(tmp_component ${CVEXTERN_DEPENDENCY_DLL_NAMES})
	LIST(APPEND UNMANAGED_DLL_32BIT "${UNMANAGED_LIBRARY_OUTPUT_PATH}/${tmp_component}.dll")
      ENDFOREACH()
      SET (HAS_WINDESKTOP_X86_NATIVE_BINARY ON)
	  
      IF (${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER} MATCHES "^$")
	SET(DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER "")
      ELSE()
	#MESSAGE(STATUS "UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER: ${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER}")
	#remove leading slashes
	STRING(REGEX REPLACE "^/" "" DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER ${UNMANAGED_LIBRARY_OUTPUT_SUBFOLDER})
	#add tailing slashes
	SET(DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER "${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}\\")
	#replace single slashes with quard slashes
	STRING(REGEX REPLACE "\\\\" "\\\\\\\\" DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER ${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER})
	#MESSAGE(STATUS "DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER: ${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}")
      ENDIF()
	  
    ELSE()
      
      FILE(GLOB UNMANAGED_DLL_32BIT "${CMAKE_CURRENT_SOURCE_DIR}/libs/x86/*.dll")	
      LIST(LENGTH UNMANAGED_DLL_32BIT UNMANAGED_DLL_32BIT_LENGTH)
      IF(UNMANAGED_DLL_32BIT_LENGTH GREATER 0)
	SET (HAS_WINDESKTOP_X86_NATIVE_BINARY ON)
	SET(DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER "x86\\\\")
      ENDIF()
    ENDIF()
    
    MESSAGE(STATUS "HAS_WINDESKTOP_X86_NATIVE_BINARY: ${HAS_WINDESKTOP_X86_NATIVE_BINARY}")
    #FOREACH (file ${UNMANAGED_DLL_32BIT})
    #	MESSAGE(STATUS "WILL DEPLOY ${file} with DEBUGGER VISUALIZER")
    #ENDFOREACH()
    IF (HAS_WINDESKTOP_X86_NATIVE_BINARY)
      SET(DEBUGER_VISUALIZER_FILES 
	"Emgu.CV.World.Netstandard"
	"Emgu.CV.UI"
	"ZedGraph")
	#"Emgu.CV.DebuggerVisualizers")
      #SET(DEBUGGER_VISUALIZER_UNMANAGED_FILES
	#    "Emgu.CV.DebuggerVisualizers"
	#${OPENCV_LIBRARY_OUTPUT_NAMES}
	#${CVEXTERN_DEPENDENCY_DLL_NAMES})
    
	SET(DEBUGGER_VISUALIZER_UNMANAGED_FILES)
	FOREACH(full_file_name ${UNMANAGED_DLL_32BIT})
		get_filename_component(just_the_file_name "${full_file_name}" NAME_WE)
		LIST(APPEND DEBUGGER_VISUALIZER_UNMANAGED_FILES "${just_the_file_name}")
    ENDFOREACH()
	  
		#MESSAGE(STATUS "########### UNMANAGED_DLL_32BIT: ${UNMANAGED_DLL_32BIT}")
	#MESSAGE(STATUS "########### DEBUGGER_VISUALIZER_UNMANAGED_FILES: ${DEBUGGER_VISUALIZER_UNMANAGED_FILES}")
		

      
      #MESSAGE(STATUS "########### DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER: ${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}")
	  
      #FOREACH(UNMANAGE_DLL_NAME ${CVEXTERN_DEPENDENCY_DLL_NAMES})
      #  LIST(APPEND DEBUGGER_VISUALIZER_FILES "${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}${UNMANAGE_DLL_NAME}")
      #ENDFOREACH()
      #MESSAGE(STATUS "DEBUGGER VISUALIZER FILES: ${DEBUGER_VISUALIZER_FILES}")
      
      MACRO(INSTALL_DEBUGER_VISUALIZER vs_version vs_description hklm_key)
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND "installDebuggerVisualizer4${vs_version}:
	ClearErrors
    Var /GLOBAL ${vs_version}_INSTALL_FOLDER ")
	
	IF("${vs_version}" STREQUAL "VS2017")
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND 
	"${INSTALL_DEBUGER_VISUALIZER_COMMAND}
    ReadRegStr \\\$R1 HKLM '${hklm_key}' '15.0'
    StrCpy $${vs_version}_INSTALL_FOLDER '\\\$R1Common7\\\\Packages\\\\Debugger\\\\Visualizers\\\\'")
	ELSEIF("${vs_version}" STREQUAL "VS2019")
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND 
	"${INSTALL_DEBUGER_VISUALIZER_COMMAND}
    ExecDos::exec /NOUNLOAD /TOSTACK '\\\$INSTDIR\\\\miscellaneous\\\\vswhere.exe -version [16.0,17.0) -property installationPath' ''
	Pop $0 
	StrCmp $0 0 0 installDebuggerVisualizer4${vs_version}Done
	Pop $1
	;MessageBox MB_OK 'vswhere result ($1).'
	StrCmp $1 0 installDebuggerVisualizer4${vs_version}Done 0
	StrCpy $${vs_version}_INSTALL_FOLDER '\\\$1\\\\Common7\\\\Packages\\\\Debugger\\\\Visualizers\\\\'
	")	
	ELSE()
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND 
	"${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	ReadRegStr \\\$R1 HKLM '${hklm_key}' 'InstallDir'
	StrCpy $${vs_version}_INSTALL_FOLDER '\\\$R1..\\\\Packages\\\\Debugger\\\\Visualizers\\\\'")
	ENDIF()
	
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND 
	"${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	IfErrors installDebuggerVisualizer4${vs_version}Done
	MessageBox MB_YESNO 'Install Debugger Visualizer for ${vs_description}?' IDNO installDebuggerVisualizer4${vs_version}Done")
	
	#Install Emgu CV managed DLLs
	FOREACH (file ${DEBUGER_VISUALIZER_FILES})
	  SET(INSTALL_DEBUGER_VISUALIZER_COMMAND
	    "${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	CopyFiles /SILENT '\\\$INSTDIR\\\\bin\\\\${file}.dll' '$${vs_version}_INSTALL_FOLDER'")
	ENDFOREACH()
	
	#INSTALL native DLLs
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND
	  "${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	CreateDirectory '$${vs_version}_INSTALL_FOLDER${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}'")
	FOREACH (file ${DEBUGGER_VISUALIZER_UNMANAGED_FILES})
	  SET(INSTALL_DEBUGER_VISUALIZER_COMMAND
	    "${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	CopyFiles /SILENT '\\\$INSTDIR\\\\libs\\\\${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}${file}.dll' '$${vs_version}_INSTALL_FOLDER${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}'")
	#MESSAGE(STATUS " --- Adding libs\${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}${file}.dll to debuger visualizer folder '$${vs_version}_INSTALL_FOLDER${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}' for ${vs_description}")
	ENDFOREACH()
	
	#IF(DEFINED UNIVERSAL_BUILD)
	#SET(INSTALL_DEBUGER_VISUALIZER_COMMAND
	#  "${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	#CreateDirectory '$${vs_version}_INSTALL_FOLDERx86\\\\'")
	#FOREACH (file ${UNMANAGED_DLL_32BIT})
	#  GET_FILENAME_COMPONENT(UNMANAGED_FILE_NAME_TO_DEPLOY ${file} NAME)
	#  MESSAGE(STATUS "Adding ${UNMANAGED_FILE_NAME_TO_DEPLOY} to debuger visualizer for ${vs_description}")
	#  SET(INSTALL_DEBUGER_VISUALIZER_COMMAND
	#    "${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	#CopyFiles /SILENT '\\\$INSTDIR\\\\libs\\\\x86\\\\${UNMANAGED_FILE_NAME_TO_DEPLOY}' '$${vs_version}_INSTALL_FOLDERx86\\\\'")
	#ENDFOREACH()
	#ENDIF()
	
	#Install the Debugger Visualizer itself
	SET(INSTALL_DEBUGER_VISUALIZER_COMMAND
	  "${INSTALL_DEBUGER_VISUALIZER_COMMAND}
	CopyFiles /SILENT '\\\$INSTDIR\\\\bin\\\\Emgu.CV.DebuggerVisualizers.dll' '$${vs_version}_INSTALL_FOLDER'
	installDebuggerVisualizer4${vs_version}Done:")
	
      ENDMACRO()
      
      MACRO(UNINSTALL_DEBUGER_VISUALIZER vs_version hklm_key)
	SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND "uninstallDebuggerVisualizer4${vs_version}:
	ClearErrors")
	
	IF("${vs_version}" STREQUAL "VS2017")
	SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	"${UNINSTALL_DEBUGER_VISUALIZER_COMMAND} 
	ReadRegStr \\\$R1 HKLM '${hklm_key}' '15.0'
	StrCpy $${vs_version}_INSTALL_FOLDER '\\\$R1Common7\\\\Packages\\\\Debugger\\\\Visualizers\\\\'")
		ELSEIF("${vs_version}" STREQUAL "VS2019")
	SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND 
	"${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
    ExecDos::exec /NOUNLOAD /TOSTACK '\\\$INSTDIR\\\\miscellaneous\\\\vswhere.exe -version [16.0,17.0) -property installationPath' '' '$EXEDIR\\\\execdos.log'
	Pop $0 
	StrCmp $0 0 0 uninstallDebuggerVisualizer4${vs_version}Done
	Pop $1
	StrCmp $1 0 uninstallDebuggerVisualizer4${vs_version}Done 0
	StrCpy $${vs_version}_INSTALL_FOLDER '\\\$1\\\\Common7\\\\Packages\\\\Debugger\\\\Visualizers\\\\'
	")	
	ELSE()
	SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	"${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
    ReadRegStr \\\$R1 HKLM '${hklm_key}' 'InstallDir'
    StrCpy $${vs_version}_INSTALL_FOLDER '\\\$R1..\\\\Packages\\\\Debugger\\\\Visualizers\\\\'")
	ENDIF()
	
	SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	"${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
	IfErrors uninstallDebuggerVisualizer4${vs_version}Done")
	FOREACH (file ${DEBUGER_VISUALIZER_FILES})
	  SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	    "${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
	Delete '$${vs_version}_INSTALL_FOLDER${file}.dll'")
	ENDFOREACH()
	
	FOREACH (file ${DEBUGGER_VISUALIZER_UNMANAGED_FILES})
	  SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	    "${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
	Delete '$${vs_version}_INSTALL_FOLDER${DEBUGGER_VISUALIZER_UNMANAGED_FILES_SUBFOLDER}${file}.dll'")
	ENDFOREACH()
	
	#IF(DEFINED UNIVERSAL_BUILD)
	#FOREACH (file ${UNMANAGED_DLL_32BIT})
	#  GET_FILENAME_COMPONENT(UNMANAGED_FILE_NAME_TO_DEPLOY ${file} NAME)
	#  SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	#    "${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
	#Delete '$${vs_version}_INSTALL_FOLDERx86\\\\${UNMANAGED_FILE_NAME_TO_DEPLOY}'")
	#ENDFOREACH()
	#ENDIF()
	
	
	SET(UNINSTALL_DEBUGER_VISUALIZER_COMMAND
	  "${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}
	#Delete '$${vs_version}_INSTALL_FOLDEREmgu.CV.DebuggerVisualizers.${vs_version}.dll'
	Delete '$${vs_version}_INSTALL_FOLDEREmgu.CV.DebuggerVisualizers.dll'
	uninstallDebuggerVisualizer4${vs_version}Done:
	")
      ENDMACRO()
      
      #INSTALL_DEBUGER_VISUALIZER("VS2008" "Visual Studio 2008" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\9.0")
      #SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #INSTALL_DEBUGER_VISUALIZER("VS2010" "Visual Studio 2010" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\10.0")
      #SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #INSTALL_DEBUGER_VISUALIZER("VS2012" "Visual Studio 2012" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\11.0")
      #SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #INSTALL_DEBUGER_VISUALIZER("VS2013" "Visual Studio 2013" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\12.0")
      #SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #INSTALL_DEBUGER_VISUALIZER("VS2015" "Visual Studio 2015" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\14.0")
      #SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
	  INSTALL_DEBUGER_VISUALIZER("VS2017" "Visual Studio 2017" "SOFTWARE\\\\\\\\WOW6432NODE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\SxS\\\\\\\\VS7")
      SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
	  
	  INSTALL_DEBUGER_VISUALIZER("VS2019" "Visual Studio 2019" "")
      SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\n${INSTALL_DEBUGER_VISUALIZER_COMMAND}")
	  
      #UNINSTALL_DEBUGER_VISUALIZER("VS2008" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\9.0")
      #SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #UNINSTALL_DEBUGER_VISUALIZER("VS2010" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\10.0")
      #SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #UNINSTALL_DEBUGER_VISUALIZER("VS2012" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\11.0")
      #SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #UNINSTALL_DEBUGER_VISUALIZER("VS2013" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\12.0")
      #SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
      
      #UNINSTALL_DEBUGER_VISUALIZER("VS2015" "SOFTWARE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\14.0")
      #SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
	  
	  UNINSTALL_DEBUGER_VISUALIZER("VS2017" "SOFTWARE\\\\\\\\WOW6432NODE\\\\\\\\Microsoft\\\\\\\\VisualStudio\\\\\\\\SxS\\\\\\\\VS7")
      SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
	  
	  UNINSTALL_DEBUGER_VISUALIZER("VS2019" "")
      SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n${UNINSTALL_DEBUGER_VISUALIZER_COMMAND}")
    ELSE()
      MESSAGE(STATUS "x86 binary not found, will not deploy debugger visualizer")
    ENDIF()
  ENDIF()
  
  # ----------------------------------------------------------------------------
  #  Build the documents of Emgu CV
  # ----------------------------------------------------------------------------
  SET(EMGU_CV_DOCUMENTATION_BUILD OFF CACHE BOOL "Build Emgu CV Documentation")
  IF(EMGU_CV_DOCUMENTATION_BUILD)
    ADD_SUBDIRECTORY(miscellaneous)
  ENDIF()

  # ----------------------------------------------------------------------------
  #  Set if we should sign the managed assembly
  # ----------------------------------------------------------------------------  
  IF (WIN32)
	SET(EMGU_SIGN_ASSEMBLY OFF CACHE BOOL "If enabled, we will sign the managed assembly")
  ELSE()
	SET(EMGU_SIGN_ASSEMBLY ON CACHE BOOL "If enabled, we will sign the managed assembly")
  ENDIF()
  
  # ----------------------------------------------------------------------------
  #  Build the extra components of Emgu CV
  # ----------------------------------------------------------------------------
  SET(EMGU_CV_EXTRA_BUILD OFF CACHE BOOL "Build Emgu CV Extra")
  IF(EMGU_CV_EXTRA_BUILD)
    ADD_SUBDIRECTORY(Emgu.RPC)
  ENDIF()
  
  # ----------------------------------------------------------------------------
  #  The source files
  # ----------------------------------------------------------------------------
  INSTALL(
    DIRECTORY
    ${CMAKE_CURRENT_SOURCE_DIR}/lib
    DESTINATION .
    COMPONENT emgucv_source
    FILES_MATCHING 
    PATTERN "*.dll"
    PATTERN "*.txt"
    PATTERN "*.xml"
    PATTERN ".git" EXCLUDE
    PATTERN "obj" EXCLUDE
    PATTERN "CMake*" EXCLUDE
    PATTERN "Release" EXCLUDE
    PATTERN "${PROJECT_NAME}.dir" EXCLUDE
    )
  
  
  #WINDOWS STORE EXAMPLE
  IF(HAVE_WINSTORE_10_X86 OR HAVE_WINSTORE_10_X64 OR HAVE_WINSTORE_10_ARM)  
    INSTALL(
      DIRECTORY
      ${CMAKE_CURRENT_SOURCE_DIR}/Solution
      DESTINATION .
      COMPONENT emgucv_example_source
      FILES_MATCHING 
      PATTERN "Emgu.CV.Example.Windows.UWP.sln"
	  PATTERN "Windows.UWP/packages/repositories.config"
      PATTERN ".git" EXCLUDE
      PATTERN "bin" EXCLUDE
      PATTERN "Android" EXCLUDE
      PATTERN "iOS" EXCLUDE
      PATTERN "Windows.Phone" EXCLUDE
      PATTERN "Windows.Desktop" EXCLUDE
      PATTERN "CrossPlatform" EXCLUDE
	  PATTERN "Mac" EXCLUDE
      )
  ENDIF()
  
  #WIN32 and not NETFX_CORE solution files
  IF (HAVE_WINDESKTOP_X86 OR HAVE_WINDESKTOP_X64)
    INSTALL(
      DIRECTORY
      ${CMAKE_CURRENT_SOURCE_DIR}/Solution
      DESTINATION .
      COMPONENT emgucv_source
      FILES_MATCHING 
      PATTERN "Emgu.CV.sln"
      PATTERN "Emgu.CV.DebuggerVisualizers.sln"
      PATTERN ".git" EXCLUDE
      PATTERN "bin" EXCLUDE
      PATTERN "Android" EXCLUDE
      PATTERN "iOS" EXCLUDE
      PATTERN "CrossPlatform" EXCLUDE
	  PATTERN "Mac" EXCLUDE
	  PATTERN "Windows.UWP" EXCLUDE
      )
    INSTALL(
      DIRECTORY
      ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.WPF
      DESTINATION .
      COMPONENT emgucv_example_source
      FILES_MATCHING 
      PATTERN "*.cs"
      PATTERN ".git" EXCLUDE
      )
    INSTALL(
      DIRECTORY
      ${CMAKE_CURRENT_SOURCE_DIR}/Solution
      DESTINATION .
      COMPONENT emgucv_example_source
      FILES_MATCHING 
      PATTERN "Emgu.CV.Example.sln"
      PATTERN ".git" EXCLUDE
      PATTERN "bin" EXCLUDE
      PATTERN "Android" EXCLUDE
      PATTERN "iOS" EXCLUDE
      PATTERN "Windows.UWP" EXCLUDE
      PATTERN "CrossPlatform" EXCLUDE
	  PATTERN "Mac" EXCLUDE
      )
    
    # ----------------------------------------------------------------------------
    #  The example files
    # ----------------------------------------------------------------------------
    INSTALL(
      DIRECTORY
      ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.Example
      DESTINATION .
      COMPONENT emgucv_example_source
      FILES_MATCHING 
      PATTERN "*.cs"
      PATTERN "*.csproj"
      PATTERN "*.resx"
      PATTERN "*.h"
      PATTERN "*.cpp"
      PATTERN "*.resX"
      PATTERN "*.ico"
      PATTERN "*.rc"
      PATTERN "CPlusPlus/*.vcproj"
      PATTERN "CPlusPlus/*.vcxproj"
      PATTERN "*.vb"
      PATTERN "*.vbproj"
      PATTERN "*.aspx" 
      PATTERN "*.dll"
      PATTERN "*.txt"
      PATTERN "*.xml"
      PATTERN "*.xaml"
      PATTERN "*.jpg"
      PATTERN "*.png"  
      PATTERN "*.settings"
      PATTERN "*.config"
      PATTERN "tessdata/*"
      PATTERN ".git" EXCLUDE
      PATTERN "obj" EXCLUDE
      PATTERN "CMake*" EXCLUDE
      PATTERN "Release" EXCLUDE
      PATTERN "Debug" EXCLUDE
      PATTERN "RelWithDebInfo" EXCLUDE
      PATTERN "*.dir" EXCLUDE
      PATTERN "Android" EXCLUDE
      PATTERN "iOS" EXCLUDE
      PATTERN "Mac" EXCLUDE
      PATTERN "WindowsStore" EXCLUDE
      PATTERN "WindowsPhoneApp" EXCLUDE
      PATTERN "WindowsStoreCameraApp" EXCLUDE
      )
  ENDIF()
  
  INSTALL(
    FILES
    "${OPENCV_SUBFOLDER}/data/haarcascades/haarcascade_frontalface_default.xml"
    "${OPENCV_SUBFOLDER}/data/haarcascades/haarcascade_eye.xml"
    DESTINATION opencv/data/haarcascades
    COMPONENT emgucv_example_source
    )
  INSTALL(
    FILES
    "${OPENCV_CONTRIB_SUBFOLDER}/modules/text/samples/trained_classifierNM1.xml"
    "${OPENCV_CONTRIB_SUBFOLDER}/modules/text/samples/trained_classifierNM2.xml"
    "${OPENCV_CONTRIB_SUBFOLDER}/modules/text/samples/trained_classifier_erGrouping.xml"
    DESTINATION opencv_contrib/modules/text/samples/
    COMPONENT emgucv_example_source
    ) 
  
  # ----------------------------------------------------------------------------
  #  Build the package
  # ----------------------------------------------------------------------------
  
  set(CPACK_COMPONENTS_ALL 
    libs #opencv components 
    emgucv_binary 
    emgucv_source
    emgucv_example_source
    )
  
  set(CPACK_PACKAGE_EXECUTABLES "" "") #http://public.kitware.com/Bug/view.php?id=7828
  
  SET(CPACK_NSIS_CONTACT "support@emgu.com")
  
  # Define MUI_TEMP that will be used for uninstalling menulinks
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP")
  
  # ----------------------------------------------------------------------------
  #  Add menu link for documentations
  # ----------------------------------------------------------------------------
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateDirectory \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation\\\" ")
  
  #SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation\\\\Open CV Documentation.lnk\\\"  \\\"$INSTDIR\\\\doc\\\\opencv2refman.pdf\\\" ")	
  #SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation\\\\Open CV Documentation.lnk\\\" ")
  
  IF(EMGU_CV_DOCUMENTATION_BUILD)
    LIST(APPEND CPACK_COMPONENTS_ALL emgucv_document)
    set(CPACK_COMPONENT_EMGUCV_DOCUMENT_DISPLAY_NAME "Emgu CV Documentation")
    set(CPACK_COMPONENT_EMGUCV_DOCUMENT_DEPENDS emgucv_binary)
    SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation\\\\Emgu CV Documentation.lnk\\\"  \\\"$INSTDIR\\\\Emgu.CV.Documentation.chm\\\" ")	
    SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation\\\\Emgu CV Documentation.lnk\\\" ")
  ENDIF()
  
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\nRMDir  \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation\\\" ")
  
  # ----------------------------------------------------------------------------
  #  Add menu link for web sites
  # ----------------------------------------------------------------------------
  LIST(APPEND CPACK_NSIS_MENU_LINKS "http://www.emgu.com" "Emgu CV wiki")
  LIST(APPEND CPACK_NSIS_MENU_LINKS "http://www.emgu.com/forum" "Emgu CV Discussion Forums")
  LIST(APPEND CPACK_NSIS_MENU_LINKS "https://github.com/emgucv/emgucv/issues" "Emgu CV Bug Tracking")
  
  # ----------------------------------------------------------------------------
  #  Add menu link for Visual Studio solutions 
  # ----------------------------------------------------------------------------
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateDirectory \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Visual Studio Solution\\\" ")
  
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Visual Studio Solution\\\\Visual Studio 2013 - 2019 Examples.lnk\\\"  \\\"$INSTDIR\\\\Solution\\\\Windows.Desktop\\\\Emgu.CV.Example.sln\\\" ")	
  
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Visual Studio Solution\\\\Visual Studio 2013 - 2019 Examples.lnk\\\" ")
  
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\nRMDir  \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Visual Studio Solution\\\" ")
  
  # ----------------------------------------------------------------------------
  #  Add menu link for Licenses 
  # ----------------------------------------------------------------------------
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateDirectory \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\License\\\" ")
  
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\License\\\\Emgu CV License.lnk\\\"  \\\"$INSTDIR\\\\${LICENSE_FILE_NAME}\\\" ")	
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\License\\\\Open CV License.lnk\\\"  \\\"$INSTDIR\\\\lib\\\\opencv.license.txt\\\" ")
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\License\\\\ZedGraph License.lnk\\\"  \\\"$INSTDIR\\\\lib\\\\3rdParty\\\\ZedGraph.license.txt\\\" ")	
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\License\\\\Tesseract OCR License.lnk\\\"  \\\"$INSTDIR\\\\lib\\\\3rdParty\\\\tesseract-ocr.license.txt\\\" ")	
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}\nCreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\License\\\\CvBlob License.lnk\\\"  \\\"$INSTDIR\\\\lib\\\\3rdParty\\\\cvblob.license.txt\\\" ")	
  
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\License\\\\Emgu CV License.lnk\\\" ")
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\License\\\\Open CV License.lnk\\\" ")
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\License\\\\ZedGraph License.lnk\\\" ")
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\License\\\\Tesseract OCR License.lnk\\\" ")
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\n Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\License\\\\CvBlob License.lnk\\\" ")
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "${CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS}\nRMDir  \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\License\\\" ")
  
  #IF(EMGU_CV_EXAMPLE_BUILD AND NOT NETFX_CORE)
  #  LIST(APPEND CPACK_COMPONENTS_ALL emgucv_example_binary)
  #  SET(CPACK_COMPONENT_EMGUCV_EXAMPLE_BINARY_DISPLAY_NAME "Emgu CV Examples (Binary)")
  #  SET(CPACK_COMPONENT_EMGUCV_EXAMPLE_BINARY_DEPENDS emgucv_binary)
  #ENDIF()
  
  set(CPACK_COMPONENT_MAIN_DISPLAY_NAME "OpenCV Native Binary")
  set(CPACK_COMPONENT_MAIN_REQUIRED ON)
  set(CPACK_COMPONENT_EMGUCV_BINARY_DISPLAY_NAME "Emgu CV (Binary)")
  set(CPACK_COMPONENT_EMGUCV_BINARY_REQUIRED ON)
  set(CPACK_COMPONENT_EMGUCV_BINARY_DEPENDS libs)
  set(CPACK_COMPONENT_EMGUCV_SOURCE_DISPLAY_NAME "Emgu CV (Source)")
  set(CPACK_COMPONENT_EMGUCV_SOURCE_DEPENDS libs)
  set(CPACK_COMPONENT_EMGUCV_EXAMPLE_SOURCE_DISPLAY_NAME "Emgu CV Examples (Source)")
  set(CPACK_COMPONENT_EMGUCV_EXAMPLE_SOURCE_DEPENDS emgucv_source)
  
ELSEIF (APPLE)
  
  SET(CPACK_GENERATOR ZIP)
  SET(CPACK_BUNDLE_NAME ${CPACK_PACKAGE_NAME})
  
  SET(CPACK_BUNDLE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/platforms/osx/icons.icns")
  IF (IOS)
    SET(CPACK_ARCHIVE_COMPONENT_INSTALL ON) #enable components install for zip
    IF(CPACK_GENERATOR MATCHES "ZIP")
      SET(CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE 1)
    ENDIF()
    set(CPACK_COMPONENTS_ALL emgucv_binary emgucv_source emgucv_example_source)
  ENDIF()

  # ----------------------------------------------------------------------------
  #  The source files
  # ----------------------------------------------------------------------------
  
  INSTALL(
    DIRECTORY
    ${CMAKE_CURRENT_SOURCE_DIR}/lib
    DESTINATION .
    COMPONENT emgucv_source
    FILES_MATCHING 
    PATTERN "*.dll"
    PATTERN "*.txt"
    PATTERN "*.xml"
    PATTERN ".git" EXCLUDE
    PATTERN "obj" EXCLUDE
    PATTERN "CMake*" EXCLUDE
    PATTERN "Release" EXCLUDE
    PATTERN "${PROJECT_NAME}.dir" EXCLUDE
    )

  
  # ----------------------------------------------------------------------------
  #  The example files
  # ----------------------------------------------------------------------------
  INSTALL(
    DIRECTORY
    ${CMAKE_CURRENT_SOURCE_DIR}/Emgu.CV.Example
    DESTINATION .
    COMPONENT emgucv_example_source
    FILES_MATCHING 
    PATTERN "*.cs"
    PATTERN "*.csproj"
    PATTERN "*.resx"
    PATTERN "*.h"
    PATTERN "*.cpp"
    PATTERN "*.resX"
    PATTERN "*.ico"
    PATTERN "*.rc"
    PATTERN "CPlusPlus/*.vcproj"
    PATTERN "CPlusPlus/*.vcxproj"
    PATTERN "*.vb"
    PATTERN "*.vbproj"
    PATTERN "*.aspx" 
    PATTERN "*.dll"
    PATTERN "*.txt"
    PATTERN "*.xml"
    PATTERN "*.xaml"
    PATTERN "*.jpg"
    PATTERN "*.png"  
    PATTERN "*.settings"
    PATTERN "*.config"
    PATTERN "tessdata/*"
    PATTERN ".git" EXCLUDE
    PATTERN "obj" EXCLUDE
    PATTERN "CMake*" EXCLUDE
    PATTERN "Release" EXCLUDE
    PATTERN "Debug" EXCLUDE
    PATTERN "*.dir" EXCLUDE
    PATTERN "Android" EXCLUDE
    PATTERN "iOS" EXCLUDE
    )
  
  INSTALL(
    FILES
    "${OPENCV_SUBFOLDER}/data/haarcascades/haarcascade_frontalface_default.xml"
    "${OPENCV_SUBFOLDER}/data/haarcascades/haarcascade_eye.xml"
    DESTINATION opencv/data/haarcascades
    COMPONENT emgucv_example_source
    )
  
  INSTALL(
    FILES
    "${OPENCV_CONTRIB_SUBFOLDER}/modules/text/samples/trained_classifierNM1.xml"
    "${OPENCV_CONTRIB_SUBFOLDER}/modules/text/samples/trained_classifierNM2.xml"
    "${OPENCV_CONTRIB_SUBFOLDER}/modules/text/samples/trained_classifier_erGrouping.xml"
    DESTINATION opencv_contrib/modules/text/samples/
    COMPONENT emgucv_example_source
    )
  
  
  # ----------------------------------------------------------------------------
  #  Generate required common assembly file for Emgu CV project
  # ----------------------------------------------------------------------------
  SET(CPACK_BUNDLE_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/CMake/osx/Info.plist)
  FILE(WRITE ${CPACK_BUNDLE_PLIST}
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
   <key>CFBundleIdentifier</key>
   <string>com.emgu.emgucv</string>
   <key>CFBundleName</key>
   <string>${CPACK_BUNDLE_NAME}</string>
   <key>CFBundleVersion</key>
   <string>1</string>
   <key>LSMinimumSystemVersion</key>
   <string>10.6</string>
</dict>
</plist>")
  
ELSE() #LINUX
  SET(MONO_GACUTIL_EXEC "gacutil")
  SET(LINUX_CONTROL_FILE_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/cmake/linux")
  SET(EMGUCV_MANAGED_DLLS "Emgu.CV.World" "Emgu.CV.UI" "ZedGraph")
  
  SET(CLIFILELIST_CONTENT "")
  FOREACH(MANAGED_DLL ${EMGUCV_MANAGED_DLLS})
    SET(CLIFILELIST_CONTENT "${CLIFILELIST_CONTENT}/usr/${CPACK_PACKAGE_CLI_FOLDER}/${MANAGED_DLL}.dll\n")
  ENDFOREACH()
  FILE(WRITE ${LINUX_CONTROL_FILE_FOLDER}/clifilelist.txt ${CLIFILELIST_CONTENT})
  INSTALL(
    FILES
    ${LINUX_CONTROL_FILE_FOLDER}/clifilelist.txt
    DESTINATION ${CPACK_PACKAGE_CLI_FOLDER}
    COMPONENT emgucv_binary)
  
  #creating the pkg-config file
  SET(PACKAGE_CFG_REF_CONTENT "")
  FOREACH(MANAGED_DLL ${EMGUCV_MANAGED_DLLS})
    SET(PACKAGE_CFG_REF_CONTENT "${PACKAGE_CFG_REF_CONTENT} -r:\${libdir}/${MANAGED_DLL}.dll")
  ENDFOREACH()
  
  FILE(WRITE ${LINUX_CONTROL_FILE_FOLDER}/${CPACK_PACKAGE_NAME}.pc
    "prefix=/usr\nexec_prefix=\${prefix}\nlibdir=\${exec_prefix}/${CPACK_PACKAGE_CLI_FOLDER}\napidir=\${exec_prefix}/${CPACK_PACKAGE_CLI_FOLDER}\n\nName: ${CPACK_PACKAGE_NAME}\nDescription: ${CPACK_PACKAGE_DESCRIPTION_SUMMARY}\nVersion: ${CPACK_PACKAGE_VERSION}\nLibs: ${PACKAGE_CFG_REF_CONTENT}\n")
  INSTALL(
    FILES
    ${LINUX_CONTROL_FILE_FOLDER}/${CPACK_PACKAGE_NAME}.pc
    DESTINATION /usr/lib/pkgconfig
    COMPONENT emgucv_binary)
  
  SET(REMOVE_EMGU_FROM_GAC_SCRIPT "")
  FOREACH(MANAGED_DLL ${EMGUCV_MANAGED_DLLS})
    SET(REMOVE_EMGU_FROM_GAC_SCRIPT "${REMOVE_EMGU_FROM_GAC_SCRIPT} ${MANAGED_DLL}")
  ENDFOREACH()
  SET(REMOVE_EMGU_FROM_GAC_SCRIPT "for dll in${REMOVE_EMGU_FROM_GAC_SCRIPT}; do ${MONO_GACUTIL_EXEC} -u $dll; done")
  
  SET(CPACK_STRIP_FILES "")
  
  IF(NOT CPACK_GENERATOR)
    MESSAGE(STATUS "Checking for CPACK build type...")
    FIND_PROGRAM(DPKG_CMD dpkg)
    IF (DPKG_CMD)
      # ----------------------------------------------------------------------------
      #  Building .deb package for Debian 
      # ----------------------------------------------------------------------------
      MESSAGE(STATUS "FOUND dpkg: ${DPKG_CMD}")
      LIST(APPEND CPACK_GENERATOR DEB)
    ENDIF()
    
    FIND_PROGRAM(RPMBUILD_CMD rpmbuild)
    IF(RPMBUILD_CMD)
      # ----------------------------------------------------------------------------
      #  Building .rpm package  
      # ----------------------------------------------------------------------------
      MESSAGE(STATUS "FOUND rpmbuild: ${RPMBUILD_CMD}")
      LIST(APPEND CPACK_GENERATOR RPM)
    ENDIF()
  ENDIF()
  
  SET(PREINST_SCRIPT "#!/bin/sh\nmkdir -p /usr/lib/pkgconfig /usr/${CPACK_PACKAGE_CLI_FOLDER} /usr/share/opencv/haarcascades /usr/share/opencv/lbpcascades /usr/share/opencv/doc \n")
  LIST(FIND CPACK_GENERATOR "DEB" TEMP)
  IF(NOT (${TEMP} EQUAL -1))
    SET(DEBIAN_CONTROL_FILE_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/cmake/debian")
    FILE(WRITE ${DEBIAN_CONTROL_FILE_FOLDER}/preinst ${PREINST_SCRIPT}) 
    FILE(WRITE ${DEBIAN_CONTROL_FILE_FOLDER}/postinst
      "#!/bin/sh\nset -e\nif [ \"$1\" = \"configure\" ]; then\n  ldconfig\n  ${MONO_GACUTIL_EXEC} -il /usr/${CPACK_PACKAGE_CLI_FOLDER}/clifilelist.txt -package ${CPACK_PACKAGE_NAME}\nfi\n")
    FILE(WRITE ${DEBIAN_CONTROL_FILE_FOLDER}/prerm
      "#!/bin/sh\nset -e\nif [ \"$1\" = \"remove\" ] || [ \"$1\" = \"upgrade\" ] && [ -x /usr/share/cli-common/gac-package-remove ]; then\n${REMOVE_EMGU_FROM_GAC_SCRIPT}\nfi\n")
    
    SET(DEBIAN_PACKAGE_PRIORITY "optional")
    SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${DEBIAN_CONTROL_FILE_FOLDER}/preinst;${DEBIAN_CONTROL_FILE_FOLDER}/postinst;${DEBIAN_CONTROL_FILE_FOLDER}/prerm;${DEBIAN_CONTROL_FILE_FOLDER}/postrm")
    SET(CPACK_DEBIAN_PACKAGE_DEPENDS "mono-runtime (>> 2.4.2), mono-2.0-gac, libtiff5, libgeotiff2, libgtk-3-0, libgstreamer1.0-0, libavcodec54, libswscale2, libavformat54, libopenexr6, libjasper1, libdc1394-22, libv4l-0, libqt4-opengl")
    IF(EMGU_CV_WITH_TESSERACT)
      SET(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libtesseract3")
    ENDIF()
    IF(WITH_TBB)
      SET(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}, libtbb2")
    ENDIF()
  ENDIF()
  
  LIST(FIND CPACK_GENERATOR "RPM" TEMP)
  IF(NOT (${TEMP} EQUAL -1))  
    SET(CPACK_RPM_PACKAGE_LICENSE "GPL v3")
    SET(CPACK_RPM_PACKAGE_REQUIRES "mono-core >= 2.6, mono-wcf, jasper, libgeotiff, gtk2, OpenEXR, libv4l, libdc1394, qt")
    IF (WITH_TBB)
      SET(CPACK_RPM_PACKAGE_REQUIRES ", tbb")
    ENDIF()
    IF(EMGUCV_WITH_TESSERACT AND TESSERACT_FOUND)
      SET(CPACK_RPM_PACKAGE_REQUIRES ", tesseract")
    ENDIF()    
    SET(RPM_CONTROL_FILE_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/cmake/rpm")
    FILE(WRITE ${RPM_CONTROL_FILE_FOLDER}/preinst ${PREINST_SCRIPT})
    FILE(WRITE ${RPM_CONTROL_FILE_FOLDER}/postinst
      "ldconfig\n${MONO_GACUTIL_EXEC} -il /usr/${CPACK_PACKAGE_CLI_FOLDER}/clifilelist.txt -package ${CPACK_PACKAGE_NAME}\n")
    FILE(WRITE ${RPM_CONTROL_FILE_FOLDER}/prerm
      "${REMOVE_EMGU_FROM_GAC_SCRIPT}\n")
    SET(CPACK_RPM_PRE_INSTALL_SCRIPT_FILE "${RPM_CONTROL_FILE_FOLDER}/preinst")
    SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${RPM_CONTROL_FILE_FOLDER}/postinst")
    SET(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${RPM_CONTROL_FILE_FOLDER}/prerm")
    SET(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE "${RPM_CONTROL_FILE_FOLDER}/postrm")
  ENDIF()	
ENDIF()  

MESSAGE(STATUS "CPACK_GENERATOR: ${CPACK_GENERATOR}")
INCLUDE (CPack) 
